1

我正在研究一种在 Java 中创建布尔数组isPrime的方法:

boolean[] isPrime;

其中素数标记为“真”,其余标记为“假”。
虽然我在这里,但我还想计算找到的素数:

int numPrimesFound;

基本思想是使用埃拉托色尼筛。到目前为止,我的方法如下所示:

public Sieve(final int limit) {

    long startTime = System.currentTimeMillis();

    boolean[] isPrime = new boolean[limit];
    this.isPrime = isPrime;

    for (int i=3; i<=limit ;i+=2) {
        isPrime[i] = true;                        //sets all even numbers to true
    }

    isPrime[2] = true;
    numPrimesFound = 1;                           //special case of '2'

    for (int i = 3; i * i <= limit; i += 2) {
        if (isPrime[i] == true) {
            for (int j = i; i * j <= limit; j += 2) {

                isPrime[i * j] = false;           //has a multiple ==> not a prime

                numPrimesFound++;                 //doesn't work yet
            }
        }
    }

    long stopTime = System.currentTimeMillis();   //measuring execution time
    System.out.println("Sieve: " + (stopTime - startTime) + " milliseconds.")

}

所以我的问题是

numPrimesFound++:

不起作用,因为筛子不止一次将一些非素数的值设置为“假” (例如 45 bcs 3*15 = 45 和 9*5 = 45)。
那么有没有人知道我如何重写这个程序,以便它只将所有非质数设置为“假”一次

或者一般来说,任何人都可以提出使该方法更快的方法吗?

4

4 回答 4

1

这是我的版本(它只计算包含输入数的可能素因数的集合):

import java.util.BitSet;
import java.util.Scanner;

public class Main {

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter the interval limit: ");
    int limit = sc.nextInt();
    int max = (int) Math.sqrt(limit);
    long start = System.currentTimeMillis();
    BitSet intArray = new BitSet(max);

    // 1 is not prime
    intArray.set(0);

    // 2 is the first prime number, so the first step is to filter out all even numbers
    for (int i = 2; i < limit; i+=2) {
        intArray.set(i);
    }

    //all remaining number will be odd
    int currentNumber = 3;
    // i is the multiplicator and will be adjusted with the current number , in order to avoid repetition
    int i = 3;
    int temp;

    while (currentNumber <= max) {
        // flag multiple of the current prime number
        while (true) {
            temp = (currentNumber * i);
            if (temp > max) break;
            intArray.set(temp - 1);
            i += 2;
        }
        //all non-prime numbers until now are already flagged, therefore we can find the next prime number by checking the set-status.
        while (true) {
            currentNumber += 2;
            if (currentNumber > max) break;
            if (!intArray.get(currentNumber - 1)) {
                i = currentNumber;
                break;
            }
        }
    }

    int b = 0;
    for (int n = max -1 ; n > 0; n--){
        if (!intArray.get(n)){
            b = n + 1;
            break;
        }
    }
    System.out.println("There are " + (max - intArray.cardinality()) + " PRIMES and the biggest is: " + b);
    System.out.println("Time in total: " + ((System.currentTimeMillis() - start) / 1000.0) + "s");
}
}

要检查 100 个 mio 号码,它需要 ca。在我的 i7 3770k 台式电脑上为 0.7 秒。

于 2018-01-25T12:56:56.770 回答
1

如果你使用 aBitSet你可以要求它的cardinality.

public BitSet primes(final int limit) {

    long startTime = System.currentTimeMillis();
    BitSet isPrime = new BitSet(limit);
    // A bitSet starts all zeros but with a sieve - evrything must start prime.
    isPrime.flip(0, limit);

    // 0 and 1 are not prime
    isPrime.clear(0);
    isPrime.clear(1);

    for (int i = 2; i * i <= limit; i += 2) {
        if (isPrime.get(i)) {
            // Remove all multiples of i.
            for (int j = 2; i * j <= limit; j += 1) {
                isPrime.clear(i * j);
            }
        }
    }

    long stopTime = System.currentTimeMillis();   //measuring execution time
    System.out.println("Sieve: " + (stopTime - startTime) + " milliseconds.");
    return isPrime;
}

public void test() {
    BitSet primes = primes(50);
    System.out.println("Primes: " + primes);
    System.out.println("Count: " + primes.cardinality());
}

我还修复了您的逻辑中的几个错误。例如,您的内部循环正在逐步执行j2而您的外部循环并未删除所有偶数(从 开始3)。

你当然可以改进这一点——谷歌是你的朋友。:)

于 2015-12-24T17:05:25.340 回答
0

好的..这是我想出的

long startTime = System.currentTimeMillis();
int limit = 100000;
boolean[] isPrime = new boolean[limit+1];

Arrays.fill(isPrime, true);
isPrime[0] = false;
isPrime[1] = false;
int numPrimesFound = limit-1;                           

System.out.println("Sqrt is:" + Math.sqrt(limit));
for (int i=2; i < Math.sqrt(limit);i++) {
    if (isPrime[i] == true) {
        int inc = 0;
        while (true) {
            int j = (i*i) + (inc*i);
            if( j > limit) break;
            if (isPrime[j]) {
                isPrime[j]= false;
                numPrimesFound--;     
            }
            inc++;
        }
    }
}

System.out.println("Number of Primes" + numPrimesFound);
for (int i = 2; i < limit;i++) {
    if (isPrime[i]) 
        System.out.println("Prime#:" + i);
}
long stopTime = System.currentTimeMillis();   //measuring execution time
System.out.println("Sieve: " + (stopTime - startTime) + " milliseconds.");
于 2015-12-24T17:55:47.990 回答
0

你有一个困惑:

numPrimesFound++; 没问题,但它必须在循环之外 for (int j = i; i * j <= limit; j += 2)

您的主循环必须走得更远(或者您忘记了大于 sqrt(limit) 的素数

 for (int i = 3; i < limit; i += 2) 

在 Eratosthenes Sieve 中,标记多次“非质数”数字是正常的。

如果 i * j < limit 大于 int 则为 NOK(变为负数)

结果是这样的,但只有

final int limit=40000; // 50 000 is too big !

用那个替换你的内部循环,你至少可以去 1000000

 for (int z = i*2; z<limit; z+=i)
    isPrime[z] = false;           //has a multiple ==> not a prime

你可以使用 bitset

于 2015-12-24T16:51:12.397 回答