-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP10_SumOfPrimes.py
50 lines (40 loc) · 1.48 KB
/
P10_SumOfPrimes.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
# -*- coding: utf-8 -*-
"""
Created on Thu Mar 3 10:27:02 2022
@author: INDHRNA
"""
"""
Problem 10
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.
"""
from time import time
start = time()
import numpy as np
import sympy as sp
import pandas as pd
#%%%
if __name__ == '__main__':
# =============================================================================
# method 1; using explicit for loop
# =============================================================================
lis = []
i = 0
limit = 2000000
counter = 2000000
for num in np.arange(limit+1):
if sp.isprime(num):
i +=1
lis.append(num)
# if i==counter: break
print(np.sum(lis),' is sum of the primes below ',limit)
# =============================================================================
# method 2; using pandas, no explicit loop, internally cython uses loop
# =============================================================================
df = pd.Series(np.arange(limit))
dff = (df[df.apply(lambda x: sp.isprime(x))]).reset_index()
print(dff.sum())
# =============================================================================
# method 3 ; direct method using sympy
# =============================================================================
print(f'sum of the primes below {counter} value is : {sum(sp.primerange(0, counter))}')