-
Notifications
You must be signed in to change notification settings - Fork 0
/
p10.py
81 lines (59 loc) · 1.89 KB
/
p10.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import numpy as np
import sys
print(" // ProjectEuler problem 10 \\")
print()
print("Find the sum of all the primes below two million.")
print("---------------------------------------------------------------------------")
print()
def checkIfPrime(number, array):
"""
Check if index is True, i.e. number is a prime number.
A False indicate that the number is not a prime number.
"""
if array[0][number]:
return True
else:
return False
def setAsNotPrime(number, array):
"""
Marks number in array as False, i.e. not a prime number.
"""
array[0][number] = False
return array
def markCompositeNumber(prime, array):
"""
Mark composite numbers in array as False, i.e. not a prime number.
"""
markNumber = prime + prime
while markNumber < len(array[0]):
array = setAsNotPrime(markNumber, array)
markNumber += prime
return array
def getPrimeNumberArray(array):
"""
Return an array with only the prime numbers
"""
primes = np.array([])
for number in range(1, len(array[0])-1):
if array[0][number]:
primes = np.append(primes, number)
return primes
def getPrimes(stop_number):
# Set all numbers as potentially prime number, including 0
primes = np.full((1, stop_number+1), True, dtype=bool)
# Mark 0 and 1 as not prime
primes = setAsNotPrime(0, primes)
primes = setAsNotPrime(1, primes)
# Check if number is prime and mark composite numbers
for number in range(2, len(primes[0]-1)):
if checkIfPrime(number, primes):
primes = markCompositeNumber(number, primes)
return getPrimeNumberArray(primes)
def sumNumbers(array):
sum = 0
for number in range(0, len(array)):
sum += array[number]
return sum
primes = getPrimes(2000000)
sum = sumNumbers(primes)
print("Sum of primes: ", sum)