-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem 357
More file actions
102 lines (84 loc) · 2.1 KB
/
Problem 357
File metadata and controls
102 lines (84 loc) · 2.1 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
Had some crazy problems using python..
Here's my java printouts
prime loaded
load size:5761455
prime loaded time: 2576ms
prime re loaded
re load size:335534
prime loaded time: 15204ms
sum = 1739023853137
Total execution time: 37109ms
And here's what i got running python
loaded primes: len = 5761455
49.328213691711426 s
loaded primes: len = 335534
582.0822110176086 s
0
100000
200000
300000
[1, 2, 6...]
1739023853137
1410.7866961956024 s
Guess python really don't fit into this problem..
1. generate all primes under 100,000,000 using sieve method
2. filter out primes, if 2+d/2 is not prime (d is already x-1 from sieve primes)
3. filter out primes if they are multiples of the 100 primes squared
4. now the primes will be added if they are generators for factors..
python code goes here, but it's really slow
Python
Hide Code
def sieveE(n):
'''
generate primes using sieve of eratosthenes method
:param n: prime up to n
:return: list containing all primes less than n
'''
A = [True] * (n + 1)
A[0] = A[1] = False
for i in range(2, int(math.sqrt(n) + 1)):
if A[i]:
j = i * i
while (j <= n):
A[j] = False
j += i
return [i for i in range(0, n + 1) if A[i]]
def factors(n):
for x in range(1,int(math.sqrt(n))+1):
if n%x==0:
if not isPrime(x + n/x):
return False
return True
s = time.time()
g =[]
x = sieveE(100000000)
print('loaded primes: len = %d' % len(x))
print(time.time()-s)
x[:] = [a-1 for a in x]
h = sieveE(100)
h[:] = [m*m for m in h]
A = [True]*len(x)
c = 0
for i in range(len(x)):
# all x are even
if not isPrime(2 + x[i]/2):
A[i] = False
for i in range(len(x)):
if A[i] and any(x[i] % f == 0 for f in h):
A[i] = False
xnew = []
for i in range(len(x)):
if A[i] == True:
xnew.append(x[i])
print('loaded primes: len = %d' % len(xnew))
print(time.time()-s)
c = 0
for i in range(len(xnew)):
if c % 1e5 == 0:
print(c)
if factors(xnew[i]):
g.append(xnew[i])
c += 1
print(g)
print(sum(g))
print(time.time()-s)