■ 메르센 소수(Mersenne Prime) 찾기

------------------------------------------------------------------------------------------------------------------------


TestProject.zip


MainForm.cs

 

 

using System;

using System.Windows.Forms;

 

namespace TestProject

{

    /// <summary>

    /// 메인 폼

    /// </summary>

    public partial class MainForm : Form

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

 

            #region 이벤트를 설정한다.

 

            this.findButton.Click += findButton_Click;

 

            #endregion

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Private

        //////////////////////////////////////////////////////////////////////////////// Event

 

        #region 찾기 버튼 클릭시 처리하기 - findButton_Click(sender, e)

 

        /// <summary>

        /// 찾기 버튼 클릭시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void findButton_Click(object sender, EventArgs e)

        {

            this.listBox.Items.Clear();

 

            Cursor = Cursors.WaitCursor;

 

            Refresh();

 

            try

            {

                checked

                {

                    long power = 1;

 

                    for(int n = 1; n < 63; n++)

                    {

                        power *= 2;

 

                        if(IsMersennePrime(power - 1))

                        {

                            this.listBox.Items.Add(n.ToString() + ": " + (power - 1).ToString());

 

                            Refresh();

                        }

                    }

                }

            }

            catch

            {

            }

 

            Cursor = Cursors.Default;

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////// Function

 

        #region 메르센 소수 여부 구하기 - IsMersennePrime(number)

 

        /// <summary>

        /// 메르센 소수 여부 구하기

        /// </summary>

        /// <param name="number">숫자</param>

        /// <returns>메르센 소수 여부</returns>

        private bool IsMersennePrime(long number)

        {

            long sqrt = (long)Math.Sqrt(number);

 

            for(long i = 3; i < sqrt; i += 2)

            {

                if(number % i == 0)

                {

                    return false;

                }

            }

 

            return true;

        }

 

        #endregion

    }

}

 

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG