【问题标题】:JAVA Program to find largest prime-factor, but the output is wrong?JAVA程序找到最大素因子,但输出错误?
【发布时间】:2019-06-19 09:24:15
【问题描述】:

我已经返回代码(在 Java 中)以找到给定数字的最大素因子

我发现检查了所有因素,然后检查它是否是素数……如果是,则打印最大的素数。

import java.util.Scanner;

public class Problem3 {


    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number");
        int n = Integer.parseInt(sc.next()); // Takes input from the user.
        int p=0,i,j,max=0,c=0;

        for(i=1;i<n;i++) {
            if(n%i != 0) { //Checks for factors and assigns that value to "c"
                c = i;
                for(j=1;j<c;j++) {
                    if(c%j==0) { //checks for prime number or not, if so... assign that value to "p"
                        p = j;
                    }

                    if(max<p) { // Checks for largest Prime factors, and assigns that value to "max"
                        max = p;
                    }
                }
            }

        }

        System.out.println(max); // prints the maximum prime-factor value.
        sc.close();
    }

}

我预计14的输出是7,但实际输出是1

【问题讨论】:

  • 我尝试输入“13195”,我得到“2639”作为输出....但是 2639 不是素数!

标签: java primes factors


【解决方案1】:

您的 for 循环中似乎有错误。我已经为此使用 java 8 创建了一个方法。试试下面的代码,

Java 8

public static void main(String[] args) {
    try (Scanner sc = new Scanner(System.in);) {
        System.out.println("Enter the number");
        int n = Integer.parseInt(sc.next()); // Takes input from the user.

        int max = IntStream.range(0, n).filter(q -> isFactor(n, q)).filter(q -> isPrimeNumber(q)).max().getAsInt();

        System.out.println(max); // prints the maximum prime-factor value.
    } catch (Exception e) {
        // TODO: handle exception
    }

}

public static boolean isPrimeNumber(int n) {
    int i, m = 0, flag = 0;
    m = n / 2;
    if (n == 0 || n == 1) {
        return false;
    } else {
        for (i = 2; i <= m; i++) {
            if (n % i == 0) {
                flag = 1;
                return false;
            }
        }
        if (flag == 0) {
            return true;
        }
    }
    return false;
}

public static boolean isFactor(int n1, int n2) {
    if(n2==0) {
        return false;
    }
    return n1 % n2 == 0;
}

Java 7

public static void main(String[] args) {
    try (Scanner sc = new Scanner(System.in);) {
        System.out.println("Enter the number");
        int n = Integer.parseInt(sc.next()); // Takes input from the user.

        int max = 0;

        for (int i = 0; i < n; i++) {
            if (isFactor(n, i)) { // checks for factor
                if (isPrimeNumber(i)) { // checks for prime
                    if (i >= max) { // checks for max number
                        max = i;
                    }
                }
            }
        }

        System.out.println(max); // prints the maximum prime-factor value.
    } catch (Exception e) {
        // TODO: handle exception
    }

}

【讨论】:

  • 这并没有做所要求的事情:它找到最大的素数直到给定的限制。这并不意味着它也是该限制的一个因素。例如。对于9,它会找到7,而正确答案是3
  • 我犯了一个错误。现在我解决了这个问题。
  • @pop 在这里 intStream 充当您的 for 循环。它将循环 0 到 n 次。 n 是输入值。然后在其中有一个 if 条件检查该因素。在里面如果有另一个如果检查素数。最后max().getAsInt() 将返回最大值为整数。
【解决方案2】:

你的主要检查是错误的。仅当 c % j != 0 对所有 1 而言,c 才是素数

int max=0,c=0;

for(int i=1;i<n;i++) {
    if(n%i == 0) { //Checks for factors and assigns that value to "c"
        c = i;
        for(int j=2;j<c;j++) {
            if(c%j==0) { // not prime
                c = 0;
                break;
            }
        }
        if(max < c) { // if c > max, it must be > 0, which means it must be prime
            max = c;
        }
    }
}

System.out.println(max); // prints the maximum prime-factor value.

【讨论】:

  • 谢谢,我没有看到“Prime Code”,但答案仍然是错误的......我得到了“6”代表“14”。
  • @pop 我用我的循环替换了你的循环,得到了输入 14 的输出 7。
  • 谢谢,我得到了输入 14 的输出 7,但是对于另一个输入“30”,根据我的问题,我必须得到“5”(因为这是最大的素因子),但是输出为“15”,即非素数。
  • @pop 我得到 5 换 30。我不知道您正在运行哪个代码,但它不可能是此答案中的代码。
【解决方案3】:

试试这个代码

public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);
    System.out.println("Enter the number");
    int n = Integer.parseInt(sc.next()); // Takes input from the user.
    int p = 0, i, j, max = 0, c = 0;

    for (i = 1; i < n; i++) {
        if (n % i == 0) { //Checks for factors and assigns that value to "c"
            c = i;
            if (isPrime(c) && c > max) {
                max = c;
            }
        }
    }

    System.out.println(max); // prints the maximum prime-factor value.
    sc.close();
}

private static boolean isPrime(int num) {
    if (num == 0 || num == 1) {
        return false;
    }
    for (int i = 2; i < num / 2; i++) {
        if (num % i == 0) {
            return false;
        }
    }
    return true;
}

【讨论】:

  • 我尝试输入“13195”,我得到“2639”作为输出....但是 2639 不是素数!
  • @pop 我认为您使用旧版本进行了测试。你能再测试一下吗?
  • 我在“onlinegdb.com”上再次尝试(类名必须改为“Main”)并编译为JAVA程序,但输出错误为“2639”
  • @pop 我在 onlinegdb.com 中检查了我的代码,它给了我预期的输出,即 29。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-12
  • 2021-05-31
  • 2021-07-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多