Problem:
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find 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.
My Solution:
Note: You can simplifies the coding :)
- static void Main(string[] args)
- {
- bool isPrime = false;
- long limit = 2000000;
- long ans = 2;
- for (long i = 3; i < limit; i += 2)
- {
- for (long j = 2; j < i; j++)
- {
- if (i % j != 0 && i != j)
- {
- isPrime = true;
- }
- else
- {
- isPrime = false;
- break;
- }
- }
- if (isPrime)
- {
- Console.Clear();
- ans += i;
- Console.WriteLine(ans);
- }
- }
- Console.WriteLine("The sum of all the primes below " + limit + " is : " + ans);
- Console.ReadLine();
- }
Note: You can simplifies the coding :)
No comments:
Post a Comment