求一个整数N的质因数【总结】

题目,如题:

    Ex:  10:  2,    5

             15: 3,    5

             30:  2,  3, 5

             26:    2, 13

             35:   5,  7

             55:   5, 11

            225:  3,   5

第一种算法程序分析:对n进行分解质因数,应先找到一个最小的质数k,然后按下述步骤完成:
    (1)如果这个质数恰等于n,则说明分解质因数的过程已经结束,打印出即可。
    (2)如果n<>k,但n能被k整除,则应打印出k的值,并用n除以k的商,作为新的正整数你n,
      重复执行第一步。
    (3)如果n不能被k整除,则用k+1作为k的值,重复执行第一步。

   private void PrimeDev(int v_SrcInt)
        {
            for (int i = 2; i <= v_SrcInt; i ++)
            {
                while (v_SrcInt != i)
                {
                    if (v_SrcInt % i == 0)
                    {
                        if(!pv_ListPrime.Contains(i))
                            pv_ListPrime.Add(i);
                        v_SrcInt /= i;
                    }
                    else
                    {
                        break;
                    }
                }
                if (v_SrcInt == i)
                {
                    if (!pv_ListPrime.Contains(i))
                        pv_ListPrime.Add(i);
                    break;
                }
            }
        }

 

第二种算法

             算法思想:使用辗转反除法,求的商,余数,用余数判断(0),然后,用递归方法,被除数递增判断,2,3,5,7,11..........

              并使用控制结束条件: (1)余数为0,加入分子(2,3,5,7.....)

                                             (2)  已添加的最大质因子 乘以 质数(2,3,5,7,9.......)等于或者是大于被除数 ,从而不必无限制的继续判断下去

Code实现:

       private void button1_Click(object sender, EventArgs e)
        {
            pv_IntN =Convert.ToInt32( textBox1.Text);
            pv_ListPrime = new List<int>();
            PrimeDevision(pv_IntN,2);
        }
        private int pv_IntN = 0;
        private List<int> pv_ListPrime = new List<int>();
        private void PrimeDevision(int v_IntDivisor,int v_IntDivide)
        {
            int lv_IntDivRes = 0, lv_IntModeRes = 0;
            lv_IntDivRes= v_IntDivisor/v_IntDivide;  //商
            lv_IntModeRes=v_IntDivisor%v_IntDivide;   //余数
            if (lv_IntModeRes == 0)  //余数为0,说明被整除,是其中的因子
            {
                //不能含有重复的因子
                if (!pv_ListPrime.Contains(v_IntDivide))
                    pv_ListPrime.Add(v_IntDivide);
                if (v_IntDivide == 2)  //被除数
                {
                    if (lv_IntDivRes == 2) //当分母为2时,结束,被2整除
                        return;
                    else
                        PrimeDevision(lv_IntDivRes, 2);  //商继续 辗转反除法
                }
                //控制其最大的质因子,节省时间
                else if (pv_ListPrime.Max() * (v_IntDivide + 2) <= v_IntDivisor)
                {    
                    //55:5,11,当为11时,刚好相等,要Add
                    if (pv_ListPrime.Max() * (v_IntDivide + 2) == v_IntDivisor &&!pv_ListPrime.Contains(v_IntDivide+2))
                        pv_ListPrime.Add(v_IntDivide + 2);
                    else  //30:2*15,PrimeDevision(15,2)  对分开的质因子继续Select
                    {

                          //商继续 辗转反除法
                        PrimeDevision(lv_IntDivRes, 2);
                    }
                }
                else if (pv_ListPrime.Max() * v_IntDivide  == v_IntDivisor)
                {
                    if (!pv_ListPrime.Contains(v_IntDivide ))
                        pv_ListPrime.Add(v_IntDivide);
                    return;
                }
            }
            //被除数==除数,11,7,9
            else if (v_IntDivide==v_IntDivisor)
            {
                if(!pv_ListPrime.Contains(v_IntDivide+2))
                    pv_ListPrime.Add(v_IntDivisor);
                return;
            }
           //不能被整除,余数不为0,继续,递归算法
            else
            {
                if (v_IntDivide == 2)
                {
                    PrimeDevision(v_IntDivisor, 3);
                }
                else
                {
                    PrimeDevision(v_IntDivisor, v_IntDivide + 2);
                }
            }
        }

 

总结: 经过测试,没有发现Bug,如果有Bug的,请告知,不胜感激!

posted @ 2012-02-08 11:53  zp_Alex  阅读(3097)  评论(0编辑  收藏  举报