用C语言打印质数

时间:2018-07-28 16:00:02

标签: c dev-c++

过去3天,我一直在努力解决问题,但我一直在失败。

我正在尝试在C中打印从1到300的所有素数。(Dev C ++)

下面是代码

#include <stdio.h>
#include <conio.h>

main() {
    // 1 - 300
    register int i, j;
    for (i = 1; i <= 300; i++) {
        for (j = 2; j < i; j++) {
            if (i % j == 0) {
                break;
            } else {
                printf("\n%d", i);
                break;
            }
        }
    }
}

请帮助我,同时也帮助我阐明概念。 谢谢。

6 个答案:

答案 0 :(得分:2)

您打印数字太早了。在外循环的末尾打印数字。也就是说,在内循环之后。

你可以做

for(i=1; i<=300; i++)
{
    for(j=2; j<i; j++)
    {
        if(i%j == 0)
        {
            break;
        }
    }
    if(j>=i)
    {
            printf("\n%d",i);
    }   
}   

如果在内循环结束时,条件j<i仍然成立,则由于执行了break语句,循环被过早终止,因此该数字不是素数。

但是如果该条件为假,即j>=i为真,则数字为质数。

您也可以在标志变量的帮助下完成此操作。

您可能不需要使用register存储类说明符。请参阅this帖子。


并且不需要break语句。在您发布的程序中,

if(i%j == 0)
{
     break;
}

else
{
    printf("\n%d",i);
    break;
}

相同
if(i%j == 0)
{
    break;
}
printf("\n%d",i);

或者只是

if(i%j != 0)
{
    printf("\n%d",i);
}

答案 1 :(得分:0)

这应该现在就可以完成,但是我敢肯定,有更有效的方法可以完成这项工作:

int main() {
    int i, j;
    char is_prime; // indicator for each number if it is a prime number
    printf("2\n"); // first number in the series, and the only one witch is even
    for (i = 3; i<=300; i += 2) // When I jump in 2 numbers each time, I save the check of all the even numbers, that we knows ahead that they are not prime numbers.
    {
        is_prime = 1; // Start with an assumption that the number is prime. If we will find out that it doesn't, we will turn this value to 0.
        for (j = 3; j < i; j++)
        {
            if(i % j == 0)
            { // If this number fully divided by this `j` value, so it is not a prime number.
                is_prime = 0; // Turn this variable to indicate that this is not a prime number.
                break; // This loop is for testing the current number. Now we found out that it is not a prime number, so we can end this loop.
            }
        } // End of the inner loop that check if the current 'i' number is a prime number.
        if (is_prime) { // If we found that the current 'i' number is a prime number (if is_prime != 0)
            printf("%d\n", i); // Print the 'i' number.
        }
    }
    return 0;
}

答案 2 :(得分:0)

对于素数最大为300的素数,Eratosthenes算法的Sieve效果很好。

答案 3 :(得分:0)

生成素数

这是实现这一目标的最简单方法

for(i=2 ; i <= n ; i++)
{
    for(j=2 ; j<i ;j++)
    {
        if(i%j == 0)
            break ; 
    }
    if(i == j )
        printf("%d\n",i);
}

答案 4 :(得分:0)

#include<stdio.h>
void main()
{
    int n,i,fact,j;
    printf("Enter the Number");
    scanf("%d",&n);
    printf("Prime Numbers are: \n");
    for(i=1; i<=n; i++)
    {
        fact=0;
        for(j=1; j<=n; j++)
        {
            if(i%j==0)
                fact++;
        }
        if(fact==2)
            printf("%d " ,i);
    }
    getch();
}

答案 5 :(得分:-1)

尝试自我解决,分别输入i和j的值,就会发现错误。

1。声明一个变量int并用0(int a = 0)对其进行初始化。

2。然后在if语句的内部for循环中为每个除法增加a的值。

If(i÷j==0)
{a=a+1;//or a++
 }

3。现在退出循环,看看a的值是否仍为0,那么我是质数,否则不是!

相关问题