查找给定整数的因数 [英] Finding factors of a given integer

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

问题描述

我有这样的事情:

int f = 120;
for(int ff = 1; ff <= f; ff++){
    while (f % ff != 0){            
}

我的循环查找因子有什么问题吗?我真的很困惑 for 和 while 语句的工作原理,所以它们很可能是完全错误的.

Is there anything wrong with my loop to find factors? I'm really confused as to the workings of for and while statements, so chances are they are completely wrong.

在此之后,我将如何为上述因素分配变量?

After this, how would I go about assigning variables to said factors?

推荐答案

以下代码将返回给定数字的所有因数列表:

The following code will return a list of all factors of a given number:

public ArrayList<Integer> findFactors(int num) {        
    ArrayList<Integer> factors = new ArrayList<Integer>();

    // Skip two if the number is odd
    int incrementer = num % 2 == 0 ? 1 : 2;

    for (int i = 1; i <= Math.sqrt(num); i += incrementer) {

        // If there is no remainder, then the number is a factor.
        if (num % i == 0) {
            factors.add(i);

            // Skip duplicates
            if (i != num / i) {
                factors.add(num / i);
            }

        }
    }

    // Sort the list of factors
    Collections.sort(factors);

    return factors;
}

<小时>

此答案在两个方面改进了Sharad Dargan 的答案:

  1. 根据这个答案中使用的想法,您可以通过确定以下值来加速解决方案根据数字是偶数还是奇数递增.

  1. Based on an idea used in this answer, you can speed up the solution by determining the value to increment by, based on whether the number is even or odd.

在for循环前添加以下代码行:

Add the following line of code before the for loop:

int incrementer = num % 2 == 0 ? 1 : 2;

然后将循环的最后一部分改为:

Then change the last part of the loop to:

 i += incrementer

如果数字是奇数,它将跳过所有偶数,而不是总是加一.

If the number is odd, it then will skip all even numbers, rather than always incrementing by one no matter what.

Sharad 将上限值存储在一个变量中,然后在 for 循环中使用该变量:

Sharad stores the upper limit value in a variable and then uses that variable in the for loop:

int upperlimit = (int)(Math.sqrt(a));
...
for(int i = 1; i <= upperlimit; i+= 1)

相反,将 Math.sqrt(num) 直接放在 for 循环中并跳过上限变量:

Instead, place Math.sqrt(num) directly in the for loop and skip the upper limit variable:

for (int i = 1; i <= Math.sqrt(num); i += incrementer) {

这将允许您跳过代码的转换部分,创建更干净的代码.

This will allow you to skip the casting part of the code, creating cleaner code.

<小时>

然后可以使用一些 JUnit 测试用例:


Some JUnit test cases you can then use:

@Test
public void test12() {
    FindFactors find = new FindFactors();

    int num = 12;
    List<Integer> factors = Arrays.asList(1, 2, 3, 4, 6, 12);

    assertEquals(factors, find.findFactors(num));
}

@Test
public void test1000000() {
    FindFactors find = new FindFactors();

    int num = 1000000;
    List<Integer> factors = Arrays.asList(1, 2, 4, 5, 8, 10, 16, 20, 25, 32, 40, 50, 64, 80, 100, 125, 160, 200,
            250, 320, 400, 500, 625, 800, 1000, 1250, 1600, 2000, 2500, 3125, 4000, 5000, 6250, 8000, 10000, 12500,
            15625, 20000, 25000, 31250, 40000, 50000, 62500, 100000, 125000, 200000, 250000, 500000, 1000000);

    assertEquals(factors, find.findFactors(num));
}

@Test
public void test1() {
    FindFactors find = new FindFactors();

    int num = 1;
    List<Integer> factors = Arrays.asList(1);

    assertEquals(factors, find.findFactors(num));
}

@Test
public void test0() {
    FindFactors find = new FindFactors();

    int num = 0;
    List<Integer> factors = new ArrayList<Integer>();

    assertEquals(factors, find.findFactors(num));
}

这篇关于查找给定整数的因数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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