简体   繁体   English

Eratosthenes C ++算法筛

[英]sieve of Eratosthenes C++ algorithm

I am trying to implement this algorithm and I have having a hard time working out the algorithm to work for finding the prime numbers up to 1000. I don't really understand but my code is not giving me the correct output, if you can suggest a way I should change my code I would greatly appreciate it. 我正在尝试实现此算法,但是我很难解决该算法以找到不超过1000的素数的问题。我不太了解,但是我的代码没有给我正确的输出,如果可以的话我应该更改代码的一种方式,我将不胜感激。

#include <iostream>

using namespace std;

bool isPrime(int n);

int main() {
  int i;
  for(i = 1; i <= 1000; i++){
    if( isPrime(i)) cout << "This number " << i <<  " is a prime. " << endl;
  }
}

bool isPrime(int n){
  if(n <= 1){
    return false;
  }
  if(n == 2){
    return true;
  }
  for(int i = 2; i < n; i++){
    if(n % i == 0){
        return false;   
    }else{
        return true;
    }
  }
}

Your decision inside the for loop inside isPrime() is wrong. 您在isPrime()for循环中做出的决定是错误的。 This is a criterion to terminate the loop: 这是终止循环的标准:

if(n % i == 0){

but the else part is no reason to terminate. else部分没有理由终止。 You have to wait until the for loop finished. 您必须等待for循环完成。 Like this: 像这样:

for(int i = 2; i < n; i++){
    if(n % i == 0){
        // Here, we are sure that n can be divided by any other numbers than 1 and n.
        return false;   
    }
}

// Here, we are sure that n cannot be divided by any number 2 .. (n-1).
return true;
}  

By the way, you only have to check until the square root of n. 顺便说一句,您只需要检查直到n的平方根即可。 You can spare the rest. 您可以省去剩下的。

The problem is in the isPrime function. 问题出在isPrime函数中。

Your isPrime function says if the the first value of i (ie 2) is not divided by n then return true. 您的isPrime函数说,如果i的第一个值(即2)未除以n则返回true。 So for eg. 因此,例如。 21, 27 etc are also counted as a prime number. 21、27等也算作素数。

You can use a flag variable in the isPrime function and used it to determine whether the n is prime or not. 您可以在isPrime函数中使用标志变量,并使用它来确定n是否为素数。 Like this 像这样

boolean prime = true;
for(int counter = 2; counter <= number / 2; counter++) {
    if(number % counter == 0) {
        prime = false;
        break;
    }
}
return prime;

I don't think this is Sieve of Eratosthenes algorithm. 我认为这不是Sieve of Eratosthenes算法Sieve of Eratosthenes If you want to implement this algorithm then you can read from here . 如果要实现此算法,则可以从此处阅读。

There is problem in your isPrime function 您的isPrime函数存在问题

bool isPrime(int n){
  if(n <= 1){
    return false;
  }
  if(n == 2){
    return true;
  }
  for(int i = 2; i < n; i++){
    if(n % i == 0){
        return false;   
    }
    else{
        return true; /* this line is very dangerous. When there is odd number it is not divisible by two so the control goes to else block and you get every odd number as your prime number */
    }
  }
}

Instead use this 改用这个

bool isPrime(int n){
  if(n <= 1){
    return false;
  }
  if(n == 2){
    return true;
  }
  for(int i = 2; i < n; i++){
    if(n % i == 0){
        return false;   
    }
  }
  return true;
}

For Sieve Of Erastosthenes try this code it may help 对于Erastosthenes筛网,请尝试使用此代码,可能会有所帮助

int b;
    cout << "Enter upper limit" << endl;
    cin >> b;
    bool *x;
    x = new bool[b];
    x[2] = true;
    for (int i = 2; i < b; i++)
    {
        int count = 2;
        if (x[i])
        {
            cout << i << endl;

            while (i*count < b)
            {
                x[i*count] = false;
                count++;
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM