Project Euler Problem#10 solution in C++

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

Find the sum of all the primes below two million.

Explanation:

Sieve of Eratosthenes: algorithm steps for primes below 121 (including optimization of starting from prime’s square).

In mathematics, the sieve of Eratosthenes (Greek: κόσκινον Ἐρατοσθένους), one of a number of prime number sieves, is a simple, ancient algorithm for finding all prime numbers up to any given limit. It does so by iteratively marking as composite (i.e. not prime) the multiples of each prime, starting with the multiples of 2.[1]

The multiples of a given prime are generated starting from that prime, as a sequence of numbers with the same difference, equal to that prime, between consecutive numbers.[1] This is the sieve’s key distinction from using trial division to sequentially test each candidate number for divisibility by each prime.[2]

The sieve of Eratosthenes is one of the most efficient ways to find all of the smaller primes (below 10 million or so).[3] It is named after Eratosthenes of Cyrene, a Greek mathematician; although none of his works have survived, the sieve was described and attributed to Eratosthenes in the Introduction to Arithmetic by Nicomachus.[4]

(above explanation taken from Wikipedia)

C++ solution:

//solution is written in a flexible manner that you could compute sum of n prime numbers.

#include <iostream>

using namespace std;

int find_prime (long *numArray, int maxNum)
{
long factor = 2; // we will make 2 as the starting point.
numArray[1] = 0; // rule out 1 from our logic to avoid incorrect results.

// loop condition will check, if we are in our maximum number limit.
//maxNum is the number till there we can find the prime numbers.
while ((factor * factor) < maxNum)
{
for (int i = 2; i < maxNum; i++) // we start our itration from number 2 not from 0 or 1 to get correct results.
{
if (numArray[i] != 0) //if a number on current array index is not zero, it is a prime or we havne’t yet checked it.
{
// we are putting zeros on all multiples of prime numbers, one by one.
if (numArray[i] != factor && (numArray[i]% factor) == 0)
{
numArray[i] = 0;
}
}
}
++factor;
}// after the loop, array will have zeros on all non prime locations and prime numbers.
}

int main ()
{
int prime_count = 0;
int64_t sum = 0;
int maxNum = 0;
int i = 0;

cout << “enter max number for array: “; // you need to test some upper limit values.
cin >> maxNum;

long *myArray = new long [maxNum];

//we fill up the array till the number we want to find the smallest positive number that is evenly divisible.
for (int i = 0; i < maxNum; i++)
{
myArray[i]= i;
}

// we will get prime numbers till the maxNum by calling below funtions.
find_prime(myArray, maxNum);

for (int j = 0; j< maxNum; j++)
{
sum += myArray[j];
}

cout << “Sum of first ” << maxNum <<  ” prime numbers : ” << sum << endl;

}

About Khuram Ali

Programming... Programming and Programming...!!!

Posted on April 29, 2013, in C++, Project Euler and tagged , , , , , , , . Bookmark the permalink. 5 Comments.

  1. Hi! I also just solved this problem in C++.
    My code takes around 3-4 seconds to calculate the sum of primes under 2,000,000 . I got the answer to be: 142,913,828,922. The code is on my blog if you want to check it out!

  1. Pingback: Project Euler Problem#11 solution in C++ | Khuram Ali

  2. Pingback: Project Euler Problem#12 solution in C++ | Khuram Ali

  3. Pingback: Project Euler Problem#13 solution in C++ | Khuram Ali

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 226 other followers

%d bloggers like this: