/
Euler_7_302j1.py
101 lines (86 loc) · 2.08 KB
/
Euler_7_302j1.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
#
#
# Euler 302
#
#
from Functions import RetFact,gcd
from collections import defaultdict
from euler import miller_rabin
def isAchilles(l):
s = set(l)
prev = 0
psetlist=[]
#hasOdds=False
for v in s:
z = l.count(v)
if z < 2: return False
#if z%2==1: hasOdds=True
psetlist.append(z)
if len(set(psetlist))==1:
return False
#if hasOdds==False:
# return False
hasOdds = False
for zz in xrange(1,len(psetlist)):
if gcd(psetlist[zz],psetlist[zz-1])==1:
hasOdds=True
break
if hasOdds==False:return False
return True
def Phi(Achnum,factlist):
# powlist = "["+factlist.rstrip(",")+"]"
# powlist = list(set(map(int,powlist.strip('[]').split(','))))
Phi = Achnum
#powlist=list(set(RetFact(Phi)))
powlist=list(set(factlist))
for i in powlist:
Phi = Phi * (int(i)-1)
#Phi = Phi / int(i)
for i in powlist:
#Phi = Phi * (int(i)-1)
Phi = Phi / int(i)
return Phi
def FactorSieve(n):
n += 1
f = defaultdict(list)
for p in xrange(2, n):
if p not in f:
for i in xrange(p + p, n, p):
j, k = i, 1
while j % p == 0:
j //= p
k *= p
f[i].append(p)
if f[p]==[]:f[p]=[p]
return f
from time import time
st = time()
N=1*10**7
print "factoring integers up to ", N
#F=FactorSieve(N)
print "factor sieve complete and hogging memory..."
ctr = 0
plist=[]
for j in xrange(100000,200000):
i = j
if miller_rabin(i):continue
f = RetFact(i)
if len(f)==1:continue
A = isAchilles(f)
if not A:continue
phiA = Phi(i,f)
#fphi=RetFact(phiA)
fphi=RetFact(phiA)
B = isAchilles(fphi)
if not B:continue
#if A and B and (set(f)==set([2,3]) or set(f)==set([3,2])): print i, f,":",phiA, fphi
#if A and B and (set(f)==set([2,3])): print i, f,",",f.count(2),f.count(3),":",phiA, fphi
#if A and B: print i, len(set(f)),":", f ,":",fphi #,",",f.count(2),f.count(3),":",phiA, fphi
if A and B: print f[-1],"<",i,":", f ,": ",RetFact(f[-1]-1)
ctr+=1
plist.append(f[-1])
print
print ctr
print "time elapsed ",time()-st
print
print sorted(list(set(plist)))