Java显示数字的质因子分解(Java Display the Prime Factorizatio

2019-06-18 06:53发布

所以对于我的任务,我必须写一个程序,要求用户一个整数输入,然后打印出号的质数分解。 这是我有:

import java.util.Scanner;

public class PrimeFactor {
    public static void main(String[] args) {
        System.out.print("Enter a positive number: ");
        Scanner scanner = new Scanner (System.in);
        int number = scanner.nextInt();
        int count;
        for (int i = 2; i<=(number); i++) {
            count = 0;
            while (number % i == 0) {
                number /= i;
                count++;
                if (count == 0) {
                    continue;
                }
            }
            System.out.println(i+ "**" + count);
        }
    }
}

我现在的问题是,每当我有一样,数量15453运行它,我得到各个因素的列表,从1到100的指数时,我只想要质因子,和我被困在如何继续。

Answer 1:

你就要成功了! 移动if-continue块之外for循环。 否则,“继续”最内部的循环,而不是你想要的一个。

while (number % i == 0) {
    number /= i;
    count++;
}
if (count == 0) {
    continue;
}
System.out.println(i+ "**" + count);

或者,你可以附上System.out.println呼叫if (count != 0)因为它是继唯一的声明continue

while (number % i == 0) {
    number /= i;
    count++;
}
if (count != 0) {
    System.out.println(i+ "**" + count);
}

您对ideone程序: 链接 。



Answer 2:

public class _03_LargestPrimeFactor {

public static void main(String[] args) {

    long a = 600851475143L;

    for(int i=2; i<(a/i); i++){                         // no factors would exist beyond a/i for a particular i

        while( a%i == 0){                               // if i is a factor 
            a = a/i;                                    // divide a by i else we wont get a prime number
            System.out.print(a + " x " + i + "\n");
        }
    }

    if(a > 1)
    System.out.println("largest prime factor: " + a);
}

}

安慰:

8462696833 X 71

10086647 X 839

6857 X 1471

大素因子:6857



Answer 3:

你接近:

  1. 该声明的System.out.println必须是你的内部for循环中,只显示与count>0
  2. 卸下if(count == 0) { continue; } if(count == 0) { continue; } ,这是无用的,因为你刚刚增加count


Answer 4:

从while循环语句,并把它后,在for循环;删除if(计数== 0){}继续。 :)

for (int i = 2; i<=(number); i++) {
        count = 0;
        while (number % i == 0) {
            number /= i;
            count++;
        }
        if(count==0) continue;
        System.out.println(i+ "**" + count);
    }


Answer 5:

不知道为什么要打印的乘法两次! 下面是清理代码:

public static void printPrimeNumbers(int prime) {

    int n;

    for (int i = 2; i <= prime; i++) {
        n = 0;
        while (prime % i == 0) {
            prime /= i;
            n++;

        }

        if (n != 0) {
            for (int j = n; j > 0; j--) {
                System.out.print(i);

                if (prime != 1) {
                    System.out.print("*");
                }
            }
        }
    }
}


Answer 6:

一方面,你continue是while循环,它没有影响任何内部。 最小的解决将是

public class PrimeFactor {
    public static void main(String[] args) {
        System.out.print("Enter a positive number: ");
        Scanner scanner = new Scanner (System.in);
        int number = scanner.nextInt();
        int count;
        for (int i = 2; i<=(number); i++) {
            count = 0;
            while (number % i == 0) {
                number /= i;
                count++;
            }
            if (count == 0) {
                 continue;
            }
            System.out.println(i+ "**" + count);
        }
    }
}

但是,你有一些其他问题:

  • 您的代码是没有正确“保理”(讽刺的是,“因素”在这方面是指不分成功能
  • 变量名称选择不当
  • 您可以使用goto语句( continue在这种情况下)时if就足够了

更好的代码会

public class PrimeFactor {
    public static void main(String[] args) {
        System.out.print("Enter a positive number: ");
        Scanner scanner = new Scanner (System.in);
        printFactors(scanner.nextInt());
    }
    public static void printFactors(int product) {
        for (int factor = 2; factor <= product; factor++) {
            int exponent = 0;
            while (product % factor == 0) {
                product /= factor;
                exponent++;
            }
            if (exponent > 0) {
                System.out.println(factor+ "**" + exponent);
            }
        }
    }
}


Answer 7:

你也可以从下面的函数一些帮助。

public int getPrimeNumber(double number) {
    int j = 0;
    while (number % 2 == 0) {
        number = number / 2;
        j = 2;
    }

    for (int i = 3; i <= number; i = i + 2) {
        while (number % i == 0) {
            number = number / i;
            j = i;
        }
    }

    return j == 0 ? 1 : j;
}

这个函数会返回给定数的最大素因子。



文章来源: Java Display the Prime Factorization of a number