简体   繁体   English

为什么我的代码不打印素数?

[英]Why my code isn't printing prime numbers?

I found some codes to print prime numbers on google but it was way to advanced and couldn't understand it so, I wanted to try it myself but my code is not working.我在谷歌上找到了一些打印素数的代码,但这是一种高级方法,无法理解,所以我想自己尝试一下,但我的代码不起作用。 It is just printing 2 but i want to print all prime numbers from 1 to 50. Somebody help!!它只是打印 2,但我想打印从 1 到 50 的所有素数。有人帮忙!

 #include <stdio.h>

    int main(){
        //prime num from 1 to 50
        int k = 0;

        for(int i = 1; i <=50 ; i++){
                for(int j = 1; j <= i ; j++){
                    if(i % j == 0){
                        k++;
                    }
                    if(k == 2){
                        printf("%d\n",i);
                    }
                }
        }
        return 0;
    }

A few problems here这里有几个问题

  • x % 1 will give 0 ( 1 is not a prime number ) x % 1将给出01不是素数
  • x % x will also give 0 x % x也会给出0
  • k needs to be reset at the beginning of the i loop k需要在i循环开始时重置
  • no need of a complex count, start with 2 and check if k is zero after the j loop不需要复杂的计数,从2开始并在j循环后检查k是否为零
  • then exit the j loop if k is incremented since the number will not be a prime, no need to keep testing如果k增加,则退出j循环,因为数字不会是素数,无需继续测试

Corrected version:修正版:

 for(int i = 2; i <=50 ; i++) {
      int k = 0;
      for(int j = 2; j < i ; j++){
            if(i % j == 0){
                 k++;
                 break; // No need to go on
            }
      }
      if(k == 0){
            printf("%d\n",i);
      }
 }

There are three bugs in this code.这段代码存在三个错误。

First, you are never resetting the value of k , so after it is equal to 2 and is subsequently increased, it will never again be equal to 2. (Unless of course, you run this program so long that it overflows back to 2.) Fix this by resetting k between every iteration of i .首先,您永远不会重置k的值,因此在它等于 2 并随后增加之后,它将永远不会再次等于 2。(当然,除非您运行这个程序太久以至于它溢出回 2。 ) 通过在i的每次迭代之间重置k来解决此问题。

Second, once k is equal to 2 in the inner loop, it will continue to print every time the inner loop iterates until it either increments k again or the loop completes, which will lead to both repetitions and false positives.其次,一旦内部循环中的k等于 2,它将在每次内部循环迭代时继续打印,直到它再次增加k或循环完成,这将导致重复和误报。 Fix this by moving the check out of the inner loop and into the outer loop.通过将检查移出内循环并移入外循环来解决此问题。

Third, you start i at 1. Not only will this lead to a false positive, it is unnecessary as 1 is not a prime number (nor is it non-prime).第三,你从 1 开始i 。这不仅会导致误报,而且没有必要,因为 1 不是质数(也不是非质数)。 Fix this by starting i at 2.通过从 2 开始i来解决此问题。

Working program:工作程序:

#include <stdio.h>

int main(){
  //prime num from 1 to 50
  int k = 0;

  for(int i = 2; i <=50 ; i++){
    k = 0;
    for(int j = 1; j <= i ; j++){
      if(i % j == 0) {
        k++;
      }
    }
    if(k == 2){
      printf("%d\n",i);
    }
  }
  return 0;
}

Notice, however, that while this will print primes, it is a brute force method that is not very efficient.但是请注意,虽然这将打印素数,但它是一种不太有效的蛮力方法。 There are many more elegant and efficient ways of finding primes, the most common of which to start out with is the Sieve of Eratosthenes .有许多更优雅和有效的寻找素数的方法,其中最常见的方法是埃拉托色尼筛法

  • Your if statement should be outside the first but inside the second for loop.您的if语句应该在第一个 for 循环之外,但在第二个for循环之内。
  • You need to reset k after the prime test is over for each number.您需要在每个数字的素数测试结束后重置k

     int main(){ //prime num from 1 to 50 int k = 0; for(int i = 1; i <=50; i++){ for(int j = 1; j <= i; j++){ if(i % j == 0){ k++; } } if(k == 2){ printf("%d\n",i); } k=0; } return 0; }

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

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