1

我的代码有问题,我无法弄清楚,你能帮我找出我的错误吗?谢谢!

/// Calculate Prime
private static int countPrimes(int number){
    int count =0;
    for(int i=1; i<=MAX_PRIME; i++) {
        if(isPrime(i)){
        count++;
        }
    }
    System.out.println("Exactly "+  count + " prime numbers exist between "+number + " and 1000."); 
return number;
}

这是我的结果:在 1 到 1000 之间恰好存在 180 个素数。这是错误的,必须是 168!

4

4 回答 4

3

似乎您的隐式参数 int number 从未在您的程序中使用过。试试这个代码:

private static int countPrimes(int number){
    int count =0;
    int countPrimes=0;
    if(number<2)
       return 0;
    for(int i=2; i<=number; i++) { //start i=2, because you know that 1 is not a prime number.
       for(int j=1; j<=i; j++){
            if(i%j==0)
                count++;                
        }
        if(count ==2)
            countPrimes++;            
        count=0;
    }
    return number;
}

然后,在您的 runner 方法中,调用:

int numOfPrimes = countPrimes(10000);

在此,您可以更改要计数的数字。在这个例子中,它计算的素数最多为 10000。

int numOfPrimes = countPrimes(100);

这将计数最多 100 个素数。如果要跟踪素数,可以在程序中添加:

ArrayList<Integer> listOfPrimes = new ArrayList<Integer>();
private static int countPrimes(int number){
    int count =0;
    int countPrimes=0;
    if(number<2)
       return 0;
    for(int i=2; i<=number; i++) { //start i=2, because you know that 1 is not a prime number.
       for(int j=1; j<=i; j++){
            if(i%j==0)
                count++;                
        }
        if(count ==2){
            countPrimes++;
            listOfPrimes.add(i);
        }           
        count=0;
    }
    return number;
}

为了检索您的号码,您可以执行以下操作:

System.out.println("Exactly "+  countPrimes(number) + " prime numbers exist between "+ "1" + " and " + number + ".");
System.out.println("These numbers are: ");
for(int i =0; i<listOfPrimes.size();i++){
    System.out.println(listOfPrimes.get(i));
}
于 2012-10-18T02:03:19.283 回答
1

可以使用“埃拉托色尼筛法”找到素数。 http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes

以下程序将计算编号。给定极限的素数。

我正在接受限制 = 1000。

public class CountPrime {

public static void main(String[] args) {

boolean[] isPrime     = null;
int       Tprimes     = 0;
final int UPPER_BOUND = Integer.parseInt("1000");

/*------------------------------------------------------+
| Assuming all to prime                                 |
+------------------------------------------------------*/
isPrime = new boolean[UPPER_BOUND + 1];
for (int Tindex = 2; Tindex <= UPPER_BOUND; Tindex++) {
  isPrime[Tindex] = true;
} /*end for*/

/*------------------------------------------------------+
| Mark Non-Primes given the Upper Bound                 |
+------------------------------------------------------*/
for (int Tindex = 2; Tindex*Tindex <= UPPER_BOUND; Tindex++) {
  /*------------------------------------------------------+
  | For any prime all the multiples are non-prime         |
  +------------------------------------------------------*/
  if (isPrime[Tindex]) {
    for (int Tver = Tindex; Tindex*Tver <= UPPER_BOUND; Tver++) {
      isPrime[Tindex*Tver] = false;
    } /*end for*/
  } /*end if*/
} /*end for*/

/*------------------------------------------------------+
| Now, as we are done count the total primes            |
+------------------------------------------------------*/
for (int Tindex = 2; Tindex <= UPPER_BOUND; Tindex++) {
  if (isPrime[Tindex]) {
    Tprimes++;
  } /*end if*/
} /*end for*/
System.out.println("Total No. of Primes[" + Tprimes +
                   "], given the limit[" + UPPER_BOUND + "]");
} /*END OF main*/

} /*END OF CountPrime*/

输出:素数总数[168],给定限制[1000]

于 2012-10-18T06:34:49.710 回答
0

isPrice() 函数是什么?检查整数是否为质数的功能是什么?我认为如果你有它,你只需要这样做:

 for(int i=1; i<=MAX_PRIME; i++) {
     if(isPrime(i)){
        count++
     }
 }
 return count;
于 2012-10-18T01:45:56.773 回答
0

我认为你返回一个错误的变量而不是count,如果countPrimes意味着计算 和 之间的素数nunberMaxPrime那么你应该从 开始计数number,如下所示:

for (int i = number; i<=MAX_PRIME;i++)
于 2012-10-18T02:06:37.057 回答