Skip to main content

Project Euler Solution 10

Problem Statement#

Calculate the sum of all the primes below two million.

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

Find the sum of all the primes below two million.

Solution#

This problem is a kind of problem 3 and Problem 7.

Implementation#

#include <stdio.h>#include <math.h>
int isPrime(long long int);
int main(){    long long int number, sum;    sum = 5;    for (number = 5; number < 2000000; number += 2)    {        if (isPrime(number)) sum = sum + number;    }    printf("%lld", sum);    return 0;}
int isPrime(long long int num){    int j, num_sqrt;    num_sqrt = sqrt(num);    if (num == num_sqrt *num_sqrt) return 0;    for (j = 2; j <= num_sqrt; j++)    {        if ((num % j) == 0) return 0;    }    return 1;}

Sample Output#

Sample Output