JAVA程序找到最大的素因,但输出错误?

时间:2019-06-19 09:24:15

标签: java primes factors

我已将代码(用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

3 个答案:

答案 0 :(得分:2)

您的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
    }

}

答案 1 :(得分:1)

您的初步检查是错误的。仅当c都等于1 c % j != 0是素数。

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.

答案 2 :(得分:0)

尝试此代码

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