Find the sum of all the primes below two million. Project euler, C
So, everything seems to be working nicely, but the program doesn't give me
the correct answer. Mine is 142,915,960,832, whereas it should be
142,913,828,922. The differece is 2,131,910 (if I still can subtract
numbers on paper haha) and I have no idea where did I get those two
millions. Could anyone help me?
#include <stdio.h>
#include <math.h>
#define BELOW 2000000
int isaprime (int num);
int main (void) {
    int i;
    float sum = 0;
    for (i = 2; i < BELOW; i++) {
            if (isaprime(i) == 1) {
                    sum = sum + i;
                    printf ("\n%d\t%.1f", i, sum);
            }
    }
    getch();
    return 0;
}
int isaprime (int num) {
    int i;
    for (i = 2; i <= sqrt(num); i++) {
            if (num % i == 0) {
                    return 0;
            }
            else {
                    ;
            }
    }
    return 1;
}
 
No comments:
Post a Comment