簡體   English   中英

打印素數從2到1000

[英]Printing out Prime Numbers from 2 to 1000

我正在寫一個代碼,將2到1000之間的所有素數寫入一個名為primes.txt的文件中。 由於某種原因,我無法找出解決此問題的正確方法。

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;

public class Problem6 {

    /**
     * @param args
     * @throws FileNotFoundException 
     */
    public static void main(String[] args) throws FileNotFoundException {
        PrintWriter prw = new PrintWriter("primes.txt");
        for (int i = 2; i <= 1000; i++){
            if (checkIfPrime(i) == true){
                System.out.println(i);
                prw.println(i);
            }
        }
    }

    public static boolean checkIfPrime (int num){
        boolean isPrime = true;  
        for (int i = 2; i <= 1000; i++){
            if ( num % i == 0 ){
                isPrime = false;
            }
        }

        return isPrime;
    }
}

我只是不知道該怎么辦...請幫助謝謝!

當您將第一個數字2傳遞給checkIfPrime時會發生什么? 它將2的余數除以2(即0),錯誤地聲稱2不是素數。

在實際達到num之前,需要停止測試余數。 i進入num之前,請停止i for循環。 (實際上,您可以在i達到num平方根后停止)。

for (int i = 2; i < num; i++){

甚至

for (int i = 2; i <= Math.sqrt(num); i++){

如果您喜歡冒險,可以嘗試實現Eratosthenes篩網 ,該篩網將所有復合數字標記為任意限制(在此問題中為1000)。 然后,您只需打印其余數字-質數。

通過僅檢查質數除法可以更快地進行計算。 任何非質數都可以被小於其本身的質數整除。

    static List<Integer> primes = new ArrayList<Integer>();

public static void main(String[] args) {
    for (int i = 2; i < 10000; i++) {
        if(checkPrime(i)){
            primes.add(i);
        }
    }
    System.out.println(primes);
}

private static boolean checkPrime(int n) {
    for (Integer i : primes) {
        if(i*i > n ){
            break;
        }else if(n%i==0 )
            return false;
     }
    return true;
}

checkIfPrime(int num) for條件更改for

for (int i = 2; i < num; i++) {

順便說一句if (checkIfPrime(i) == true){可以寫成if (checkIfPrime(i)){

如果數字num不能被大於1 且小於num其他數字整除,則它是質數。 您的代碼在哪里? :-)

您可以按照以下方法2-3-5-7輪 “硬編碼” Eratosthenes增量篩,以打印多達1000的質數。 類似C的偽代碼中

primes_1000()
{
   // the 2-3-5-7 wheel
   int wh[48] = {10,2,4,2,4,6,2,6,4,2,4,6,6,2,6,4,2,6,4,6,8,4,2,4,
                  2,4,8,6,4,6,2,4,6,2,6,6,4,2,4,6,2,6,4,2,4,2,10,2};
   // core primes' multiples, each with its pointer into the wheel
   int m[7][4] = { {1,11,11,11*11}, {2,13,13,13*13}, {3,17,17,17*17},
                   {4,19,19,19*19}, {5,23,23,23*23}, {6,29,29,29*29},
                   {7,31,31,31*31} };    // 23*23 = 529 
   int i=1, p=11, k=0;
   print(2); print(3); print(5); print(7);
   p = 11;             // first number on the wheel - the first candidate
   do {
      // the smallest duplicate multiple is 121*13, ==> no dups below 1000!
      for( k=0; k < 7; ++k) {
         if ( p == m[k][3] ) {             // p is a multiple of m[k][1] prime:
            m[k][2] += wh[ m[k][0]++ ];    //   next number on the wheel
            m[k][3]  = m[k][1] * m[k][2];  //   next multiple of m[k][1] 
            m[k][0] %= 48;                 //   index into the wheel
            break;
         }
      }
      if (k == 7) {    // multiple of no prime below 32 -
          print(p);    //   - a prime below 1000!   (32^2 = 1024)
      }
      p += wh[i++];    // next number on the candidates wheel
      i %= 48;         // wrap around to simulate circular list
   } while ( p < 1000 );
}

對於小於500的素數,僅需維護4個篩網變量,對於車輪固有質數2,3,5,7上方的附加芯素數{11,13,17,19}

(另請參閱打印從1到100的質數 )。

m是輪子上基本質數及其倍數的字典( multiplesOf(p) = map( multiplyBy(p), rollWheelFrom(p) )multiplesOf(p) = map( multiplyBy(p), rollWheelFrom(p) ) ,每個素數都有其在輪子上的索引。它實際上應該是一個優先級隊列min -按倍數的值排序。

對於真正的無界解決方案,可以維持單獨的素數供給 ,以便在候選項中達到下一個素數平方時逐個素數擴展字典素數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM