项目euler#7的Java代码给出了错误的答案

时间:2015-07-24 12:38:37

标签: java

此代码旨在找到第1001个素数,但给出了答案47,这显然是错误的。

public class problem7 {

   public static void main(String[] args) {
        int[] myarray = new int[1001];
        int j = 0;
        boolean prime = false;

        for (int i = 2;; i++) {
            for (int k = 2; k < i; k++) {
                if (i == (k - 1) && i % k != 0) {
                    prime = true;
                }

                if (i % k == 0) {
                    prime = false;
                    prime = true;
                }
                if (prime) {
                    myarray[j] = i;
                }
                if (j == 1000) {
                    System.out.println(myarray[1000]);
                    return;
                }

                j++;
            }

        }
    }
}

非常感谢任何帮助。

4 个答案:

答案 0 :(得分:1)

我认为j ++只有在插入素数时才会增加。通过使用此代码,您将获得1001 Prime数

public static void main(String[] args) {
        int[] myarray = new int[1001];
        int j = 0;

        for (int i = 2;; i++) {
            boolean prime = false;
            for (int k = 2; k < i; k++) {
                if (i % k == 0) {
                    prime = true;
                }
            }
            if (!prime) {
                myarray[j] = i;
                j++;
            }
            if (j == 1001) {
                break;
            }
        }
        for (int primeNumber : myarray) {
            System.out.println(primeNumber);
        }
    }

答案 1 :(得分:1)

Your check for prime is wrong: you cannot declare a number prime and set prime = true based on a single test. The inner loop should set prime to false, but it shouldn't reset it to true: once it's false, it's false.

The algorithm should proceed as follows:

  • For each i, set prime=true
  • Loop over potential divisors k
  • If a divisor such that i % k == 0 is found, set prime = false, and break the loop
  • If prime is still true at the end of the nested loop, add i to the list of primes.

This should give you a correct result, at which point you should consider optimizing your solution using considerations below:

  • If you did not find a divisor among k below or at sqrt(i), then i is prime
  • You do not have to try all numbers k, only the ones from the list of primes that you have discovered so far.

答案 2 :(得分:0)

这是dasblinkenlights算法的实现!

public static void main(String args[]) {
    int[] primes = new int[1001];
    int i = 1;
    int index = 0;
    while (primes[1000] == 0) {
        i++;
        boolean skip = false;
        for (int i1 : primes) {
            if (i1 == 0)
                break;
            if (i % i1 == 0) { //checks if the number is a multiple of previous primes, if it is then it skips it
                skip = true;
                break;
            }
        }
        if (!skip) {
            if (isPrime(i)) {
                primes[index] = i;
                System.out.println(i);
                index++;
            }
        }
    }
}

static boolean isPrime(int n) {
    for (int i = 2; i < n; i++) {
        if (n % i == 0)
            return false;
    }
    return true;
}

答案 3 :(得分:0)

你在这里做的素性测试有点含糊不清。因为一般的方法是,我们选择任何数字假定它的素数,所以在开始时prime = true。然后,如果存在k input k < input因子kprime = false,那么该数字不是素数,那么104743

我修改了您的代码并获得了结果: i

更新:这是找到大素数的一种更快的方法。内部循环将迭代到public static void main(String[] args) { int[] myarray = new int[10001]; int j = 0; boolean prime = true; int i = 2; while (true) { prime = true; for (int k = 2; k*k <= i; k ++) { if (i % k == 0) { prime = false; } } if (prime) { myarray[j] = i; if (j == 10000) { System.out.println(myarray[10000]); return; } j++; } if(i > 4) i += 2; else i++; } } 的平方根,原因:Why do we check upto the square root of a prime number to determine if it is prime?


        case "custname":
        cc.Descendants<Text>().First().Text =