Help with Poject Euler 10?

hey so I'm working on solving all of the Project Euler problems and I'm stuck at 10. My code keeps giving me a number that's close, but i think I'm off by about 2 million. Can anyone help?

Question: The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

Find the sum of all the primes below two million.


Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <stdio.h>
#include <math.h>
#define false 0
#define true 1
typedef int bool;

bool isPrime(double x);

int main(void)
{
        int i, abc;
        double sum = 0;
        double x; // number to be checked for primeness

        for (x = 2; x < 2000000; x++)
        {
                if (isPrime(x))
                {
                        sum += x;
                        printf("%.0lf\n", sum);
                }
        }

        return 0;
}

bool isPrime(double x)
{
        if ((x != 2) && ((long)x % 2 == 0) || (x == 9)) // for numbers less than 10
                return false;
        else
        {
                long i;
                for (i = sqrt(x) - 1; i > 1; i--) // checks if sqrt(x) - 1 is divisible by any number from 2 to sqrt(x) - 1
                {
                        if ((long)x % i == 0)
                                return false;
                }
                return true;
        }
}


BTW this is written in C, not C++... sorry about that...
for (i = sqrt(x); i > 1; i--)
Rather than cast between long/double all over the place... why not just use long everywhere? You avoid rounding errors which might be causing some minor goof ups.
Topic archived. No new replies allowed.