Project Euler – Problem 10


Calculate the sum of all the primes below two million.

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

Find the sum of all the primes below two million.

My Solution:
Uses my IsPrime function from other problems

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Euler
{
    class Problem10 : IProblemBase
    {

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

        //Find the sum of all the primes below two million.

        //Note: result is long, if you use int, you will get the wrong result
        //ask me how I know...lol

        public Problem10()
        {
        }   

        public string GetAnswer()
        {
            long result = 0;
            long limit = 2000000;

            for (int i = 2; i < limit; i++)//two is the first prime
            {
                if (Util.isPrime(i))
                {
                    result = result + i;

                }

            }

            return result.ToString();

        }
    }
}
Advertisements

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

%d bloggers like this: