Harsh Bhatt
Harsh Bhatt

Reputation: 3

Sum of primes: what is the problem in result?

The code works fine but why are my answers wrong in the int result?

in output:

3
10
2 3 5 7: 17  //correct
30
2 3 5 7 11 13 17 19 23 29: 146  //incorrect
50
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47: 474   //incorrect

Here is the code:

#include <stdio.h>

int main() {
    int y, n, i, fact, j, result = 0;
    scanf("%d", &y);
    for (int x = 1; x <= y; x++) {
        scanf("%d", &n);
        for (i = 1; i <= n; i++) {
            fact = 0;
            for (j = 1; j <= n; j++) {
                if (i % j == 0)
                    fact++;
            }
            if (fact == 2) {
                result += i;
                printf("%d ", i);
            }
        }
        printf(": %d\n", result); //Not Getting correct answer please HELP!
    }
    return 0;
}

Upvotes: 1

Views: 60

Answers (2)

Vlad from Moscow
Vlad from Moscow

Reputation: 310960

The variable result is initialized only once

int y, n, i, fact, j, result = 0;

So it will accumulate values calculated in the loop

for(int x=1;x<=y;x++){
   //...
}

Move the declaration of the variable result in the body of the loop

for(int x=1;x<=y;x++){
   int result = 0;
   //...
}

To avoid such an error you should declare variables in the minimum scope where they are used.

Also this loop

        for (j = 1; j <= n; j++)
        {
            if (i % j == 0)
                fact++;
        }

does not make a great sense. Change the condition in the loop the following way

        for (j = 1; j <= i; j++)
        {
            if (i % j == 0)
                fact++;
        }

substituting the variable n for the variable i.

Also you should use an unsigned integer type instead of the signed integer type int because prime numbers are defined for natural numbers.

The program can look for example the following way

#include <stdio.h>

int main(void) 
{
    unsigned int n = 0;
    
    scanf( "%u", &n );
    
    while ( n-- )
    {
        unsigned int max_value = 0;
        
        scanf( "%u", &max_value );
        
        unsigned long long int sum = 0;
            
        for ( unsigned int i = 1; i <= max_value; i++ )
        {
            size_t count = 0;
                
            for ( unsigned int j = 1; j <= i; j++ )
            {
                if ( i % j == 0 ) ++count;  
            }
            
            if ( count == 2 )
            {
                printf( "%u ", i );
                sum += i;
            }
        }
        
        printf( ": %llu\n", sum );
    }
    
    return 0;
}

If to enter

3
10
20
100

then the output will be

2 3 5 7 : 17
2 3 5 7 11 13 17 19 : 77
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 : 1060

Upvotes: 0

MikeCAT
MikeCAT

Reputation: 75062

You forgot to initialize result before each calculation.

for(int x=1;x<=y;x++){
    scanf("%d", &n);
    result = 0; // add this for initialization
    for (i = 1; i <= n; i++) {
        /* ... */
    }
    /* ... */
}

Upvotes: 3

Related Questions