-
Notifications
You must be signed in to change notification settings - Fork 0
/
e343.py
133 lines (130 loc) · 3.72 KB
/
e343.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
from fractions import Fraction as frac
from script.memorize import memo
from script.helpers import totient as phi
from script.maths import primetree,primesd3,isprime2,nextprime
from time import time
from math import ceil
from script.maths import roots
def e343(n = 2*10**6):
alist = [1 + k**3 for k in range(n + 1)]
primes = primesd3(n + 2)
print()
for p in primes:
for i in roots(p):
for j in range(i, n + 1, p):
while alist[j] > p and alist[j] % p == 0:
alist[j] //= p
return sum(alist) - n - 1
def e343_test(top=2*10**6):
global primes
primes = primesd3(top+1)
print()
vect = [i**3+1 for i in range(1,top+1)]
result = 0
t = time()
for i in range(len(vect)):
if i%100==0:print(i)
vect[i]=max(factors(vect[i]))
vect[i]=phi(vect[i])
return sum(vect)
for k in range(1,top+1):
i = k**3+1
j = round(i**(1/3))
if j%100==0:print(j)
global primes
tmp = max(factors(i))#,primes))
result+=phi(tmp)
return result
@memo
def factors(n):
global primes
p = primes
if n ==1:return [1]
if n in p:return [n]
else:
for i in range(len(p)):
if n%p[i]==0:
result =[p[i]]+factors(n//p[i])#,primes)
return result
if isprime2(n):return [n]
else:
primes+=[nextprime(primes[-1])]
return factors(n)#,primes)
def test():
with open('script/f343.txt',encoding='utf-8') as afile:
f = afile.read()[:-1].split('\n')
flist = [i.split(':') for i in f if i!='']
cache = {}
for i in flist[:-2]:
cache[i[0][1:-1]]=int(i[1])
cache={}
count = 0
def ftest(y,x=1): #y=k on the first call
n = str(frac(x,y)).split('/')
if len(n)==1:return n
else:return f(int(n[1])-1,int(n[0])+1)
@memo
def f1(k):
if k==1:return 1
if cache.get(k):
return cache.get(k)
n = frac(1,int(k))
nx = [str(1),str(k)]
alist = []
while True:
if len(nx)==1 or nx[1]==1:
return int(nx[0])
else:x = nx[0];y = nx[1]
x=int(x)+1
y=int(y)-1
n = frac(x,y)
nx = str(n).split('/')
if nx[0]=='1':return f1(nx[1])
def f(k):
global cache
n = frac(1,k)
nx = [str(1),str(k)]
alist = []
if k==1:return 1
while True:
if nx[0]== '1' and len(nx)!=1:alist+=[nx[1]]
if len(nx)==1 or int(nx[1])==1:
for i in alist:
cache[i]=int(nx[0])
return int(nx[0])
else:x = nx[0];y = nx[1]
x=int(x)+1
y=int(y)-1
n = frac(x,y)
nx = str(n).split('/')
if nx[0]=='1' and len(nx)!=1 and cache.get(nx[1]):
global count
count+=1
for i in alist:
cache[i]=cache.get(nx[1])
return cache.get(nx[1])
def e343(tmp=2*10**6):
global cache
result = 0
b = True
for i in range(1,tmp+1):
global cache
print(i)
result+=f(i**3)
return result
try:
print(e343(100))
with open('f343.txt',mode='a',encoding='utf-8') as f:
for i in cache.keys():
f.write('\n\''+i+'\':'+str(cache.get(i)))
except:
with open('f343.txt',mode='a',encoding='utf-8') as f:
for i in cache.keys():
f.write('\n\''+i+'\':'+str(cache.get(i)))
if __name__=='__main__':
t=time()
print(e343(100))
print(time()-t)
t=time()
print(e343())
print(time()-t)