块表达式来自Eratosthenes的Sieve,孪生素数,Java中的素数

时间:2014-02-06 22:19:11

标签: java sieve-of-eratosthenes

我需要帮助,不知道如何纠正错误并允许程序运行。 计划是所有素数花费3-12345。素数和孪生素数。 所有结果必须是块表达式。 我会感谢任何帮助!

public class SieveOfEratosthenes {

    //final N and byte f[] there is provision must not be changed!

    static final int N = 12345;
    static byte f[] = new byte[(N + 1) / 2];

    public static void main(String[] args) {
    System.out.println(sieve());
    System.out.println(twinPrime());
    System.out.println(countPrime());
    }

    private static int sieve() {
    int prime;
    for (int i = 3; i < N; i++)
        return f[i] = false; //cancel all Primzahle
    /*
     * sieves with all primes i, where i is the smallest 
     * prime factor of a composite number j = k*i. 
     * The smallest prime factor of a complex number j 
     * can not be greater than the root of j <= n
     */
    for (int i = 2; i < Math.sqrt(N); i++) {
        do {
        //now i is prime
        return i;

        } while (f[i] = true);
        //and underline its multiples, starting with i * i
        for (int j = i * i; j < N; j++) {
        return f[j] = true;
        }
    }

    //Give the prime numbers greater than n from sqrt. 
    //Thus, those have not yet been deleted

    for (int i = Math.sqrt(N) + 1; i <= N; i++) {
        do {
        return i;

        } while (f[i] = true);
    }
    //now i is prime
    return i = prime;
    }

    //Twin prime
    private static int twinPrime() {
    for (int n = 1; n <= N; n++) {
        if (((6 * n - 1) % 2 != 0) && ((6 * n + 1) % 2 != 0)) {
            //mark all twin prime with "*"
        return Integer.parseInt((n + "*"));
        }
    }
    }

    //Number of primes
    private static int countPrime() {
    return Integer.bitCount(twinPrime());
    }

}

0 个答案:

没有答案