Skip to content

Commit 7beaeae

Browse files
Brute force solution to Problem 10
1 parent 8be9dfc commit 7beaeae

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

Project Euler/Problem 10/sol1.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from __future__ import print_function
2+
from math import sqrt
3+
4+
def is_prime(n):
5+
for i in xrange(2, int(sqrt(n))+1):
6+
if n%i == 0:
7+
return False
8+
9+
return True
10+
11+
def sum_of_primes(n):
12+
if n > 2:
13+
sumOfPrimes = 2
14+
else:
15+
return 0
16+
17+
for i in xrange(3, n, 2):
18+
if is_prime(i):
19+
sumOfPrimes += i
20+
21+
return sumOfPrimes
22+
23+
if __name__ == '__main__':
24+
import sys
25+
26+
if len(sys.argv) == 1:
27+
print(sum_of_primes(2000000))
28+
else:
29+
try:
30+
n = int(sys.argv[1])
31+
print(sum_of_primes(n))
32+
except ValueError:
33+
print('Invalid entry - please enter a number.')

0 commit comments

Comments
 (0)