快保理算法?算法、快保理

由网友(聋子听见哑巴说瞎子看见鬼)分享简介:我如何能快速找到一些的所有因素?例如:位数:20 因素:{1 * 20,2 * 10,4 * 5,5 * 4,* 10 2,20 * 1} 解决方案 如果您要查找的因素恰好是单数,你只需要测试奇数,因为它是不可能有一个更因素奇数。因此,用pre-清理前,你可以保存自己的一些处理。 私有静态列表< INT>...

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

例如:

  

位数: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);
        返回列表;
    }
 

通过简单的暴力求解的方式实现KNN算法

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;
    }

阅读全文

相关推荐

最新文章