有多少素数?

时间:2012-10-18 01:35:48

标签: java primes

我的代码出了点问题,我无法理解,你会帮我找出错误吗?谢谢!

/// 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 个答案:

答案 0 :(得分: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;
}

然后,在您的跑步方法中,请致电:

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));
}

答案 1 :(得分:1)

可以使用“Eratosthenes筛”找到黄金计数。 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]

答案 2 :(得分:0)

功能是什么价格()?检查整数是否为素数是一个函数吗?我想如果你拥有它,你只需要这样做:

 for(int i=1; i<=MAX_PRIME; i++) {
     if(isPrime(i)){
        count++
     }
 }
 return count;

答案 3 :(得分:0)

我认为你返回了一个错误的变量而不是count,如果countPrimes意味着计算nunberMaxPrime之间的素数计数,那么你应该从number,就像这样:

for (int i = number; i<=MAX_PRIME;i++)