如何在给定范围内找到C中的素数之和?

时间:2013-02-07 08:21:52

标签: c logic primes

我是编程的新手,我被要求使用while循环找到给定范围内的素数之和。如果输入为5,则答案应为28 (2+3+5+7+11)。我尝试编写代码,但似乎逻辑不正确。

CODE

#include <stdio.h>
int main()
{
    int range,test;
    int sum = 2;
    int n = 3;
    printf("Enter the range.");
    scanf("%i",range);
    while (range > 0)
    {
        int i =2;
        while(i<n)
        {
            test = n%i;
            if (test==0)
            {
             goto end;
            }
                  i++;
        }
        if (test != 0)
        {
            sum = sum + test;
            range--;
        }
        end:
                  n++;
    }
    printf("The sum is %i",sum);
    return 0;
}

如果你能指出我的错误并且可能告诉我如何从那里开始,那将是很好的。

5 个答案:

答案 0 :(得分:4)

首先,scanf使用&range而非range

scanf("%i",&range);

第二条指令不正确

sum = sum + test;

应该是

sum = sum + n;

以及

while (range > 0)

应改为

while (range > 1)

因为在你的算法中,你已经将范围的第一个元素放在总和sum = 2中,所以while应该循环range - 1次而不是range

这就是全部

答案 1 :(得分:0)

好的,我的C真的很糟糕,但尝试类似下面的代码。可能不编译,但如果它是一个家庭作业或其他东西,你最好自己弄明白:

更新:根据要求制作了一个while循环。

#include <stdio.h>
int main()
{
    int range, test, counter, innerCounter, sum = 1;
    int countPrimes = 1;
    int [50] primesArray;
    primesArray[0] = 1;

    printf("Enter the range.");
    scanf("%i",range);

    counter = 2;
    while (counter <= range) {
        for (innerCounter = 1; innerCounter < countPrimes; innerCounter++) {
            if (counter % primesArray[innerCounter] == 0)
                continue;
            primesArray[countPrimes + 1] = counter;
            countPrimes ++;
            sum += counter;
        }

        counter ++
    }

    printf("The sum is %i",sum);
    return 0;
}

答案 2 :(得分:0)

我有一段时间没有做过C,但我会做一些函数来简化你的逻辑:

#include <stdio.h>
#include <math.h>

int is_prime(n) {
    int i;

    for (i = 2; i <= sqrt(n); i++) {
        if (n % i == 0) {
            return 0;
        }
    }

    return 1;
}

int main() {
    int range, i, sum, num_primes = 0;

    printf("Enter the range: ");
    scanf("%d", &range);

    for (i = 2; num_primes < range; i++) {
        if (is_prime(i)) {
            sum += i;
            num_primes++;
        }
    }

    printf("The sum is %d", sum);

    return 0;
}

使用goto并将所有代码推送到main()将使您的程序难以调试。

答案 3 :(得分:0)

在这里尝试最简单的方法。检查C program to find sum of all prime between 1 and n numbers

<强> CODE

#include <stdio.h>

int main()
{
    int i, j, n, isPrime, sum=0;

    /*
     * Reads a number from user
     */
    printf("Find sum of all prime between 1 to : ");
    scanf("%d", &n);

    /*
     * Finds all prime numbers between 1 to n
     */
    for(i=2; i<=n; i++)
    {

        /*
         * Checks if the current number i is Prime or not
         */
        isPrime = 1;
        for(j=2; j<=i/2 ;j++)
        {
            if(i%j==0)
            {
                isPrime = 0;
                break;
            }
        }

        /*
         * If i is Prime then add to sum
         */
        if(isPrime==1)
        {
            sum += i;
        }
    }

    printf("Sum of all prime numbers between 1 to %d = %d", n, sum);

    return 0;
}

答案 4 :(得分:0)

复制 - 从here粘贴。

#include <stdio.h>

  int main() {
        int i, n, count = 0, value = 2, flag = 1, total = 0;

        /* get the input value n from the user */
        printf("Enter the value for n:");
        scanf("%d", &n);

        /* calculate the sum of first n prime nos */
        while (count < n) {
                for (i = 2; i <= value - 1; i++) {
                        if (value % i == 0) {
                                flag = 0;
                                break;
                        }
                }
                if (flag) {
                        total = total + value;
                        count++;
                }
                value++;
                flag = 1;
        }

        /* print the sum of first n prime numbers */
        printf("Sum of first %d prime numbers is %d\n", n, total);
        return 0;
  }

<强>输出:

Enter the value for n:5
Sum of first 5 prime numbers is 28