■ 최대공약수(GCD)/최소공배수(LCM) 구하기

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


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.btnGo.Click += runButton_Click;

 

            #endregion

        }

 

        #endregion

 

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

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

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

 

        #region 실행 버튼 클릭시 처리하기 - runButton_Click(sender, e)

 

        /// <summary>

        /// 실행 버튼 클릭시 처리하기

        /// </summary>

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

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

        private void runButton_Click(object sender, EventArgs e)

        {

            long a = long.Parse(this.txtA.Text);

            long b = long.Parse(this.txtB.Text);

 

            this.gcdTextBox.Text = GetGreatestCommonDivisor(a, b).ToString();

            this.lcmTextBox.Text = GetLeastCommonMultiple(a, b).ToString();

        }

 

        #endregion

 

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

 

        #region 최대 공약수 구하기 - GetGreatestCommonDivisor(a, b)

 

        /// <summary>

        /// 최대 공약수 구하기

        /// </summary>

        /// <param name="a">A</param>

        /// <param name="b">B</param>

        /// <returns>최대 공약수</returns>

        private long GetGreatestCommonDivisor(long a, long b)

        {

            a = Math.Abs(a);

            b = Math.Abs(b);

 

            for(; ; )

            {

                long remainder = a % b;

 

                if(remainder == 0)

                {

                    return b;

                }

 

                a = b;

                b = remainder;

            };

        }

 

        #endregion

        #region 최소 공배수 구하기 - GetLeastCommonMultiple(a, b)

 

        /// <summary>

        /// 최소 공배수 구하기

        /// </summary>

        /// <param name="a">A</param>

        /// <param name="b">B</param>

        /// <returns>최소 공배수</returns>

        private long GetLeastCommonMultiple(long a, long b)

        {

            return a * b / GetGreatestCommonDivisor(a, b);

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요