快保理算法? [英] Fast factoring algorithms?

查看:119
本文介绍了快保理算法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何能快速找到一些的所有因素?

例如:

  

位数:20
  因素:{1 * 20,2 * 10,4 * 5,5 * 4,* 10 2,20 * 1}

解决方案

如果您要查找的因素恰好是单数,你只需要测试奇数,因为它是不可能有一个更因素奇数。因此,用pre-清理前,你可以保存自己的一些处理。

 私有静态列表< INT> findFactors(INT NUM)
    {
        INT增量= 1;
        如果(NUM%2!= 0)
        {
            增量器= 2; //只考奇的人
        }
        名单< INT>名单=新的名单,其中,INT>();
        的for(int i = 1; I< = NUM​​ / 2,I = I +增量)
        {
            如果(NUM%我== 0)
            {
                list.Add(ⅰ);
            }
        }
        list.Add(NUM);
        返回列表;
    }
 

How can I quickly find all factors of a number?

e.g.:

digit: 20
factors: {1*20, 2*10, 4*5, 5*4, 10*2, 20*1}

解决方案

If the number that you want to find the factors for happens to be odd, you only need to test odd numbers because it is impossible to have an even factor for an odd number. So with a pre-check up front, you can save yourself some processing.

    private static List<int> findFactors(int num)
    {
        int incrementer = 1;
        if (num % 2 != 0)
        {
            incrementer = 2; //only test the odd ones
        }
        List<int> list = new List<int>();
        for (int i = 1; i <= num / 2; i=i+incrementer)
        {
            if (num % i == 0)
            {
                list.Add(i);
            }
        }
        list.Add(num);
        return list;
    }

这篇关于快保理算法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆