-
Notifications
You must be signed in to change notification settings - Fork 2
/
p243.py
115 lines (102 loc) · 2.33 KB
/
p243.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
#!/usr/bin/env python
# Project Euler: problem 243
# http://projecteuler.net/problem=243
import math
# functions taken from prime number library by kroq-gar78
def trialdiv(n,primes=[2]):
if(n&1==0 or n<3): return [(n==2),primes]
for i in primes:
if(n%i==0): return [(n==i),primes]
for i in xrange(primes[len(primes)-1],int(math.sqrt(n))+1):
ifprime = True
for j in primes:
if i%j == 0:
ifprime = False
break
if ifprime:
if n%i==0: return [False,primes]
if( not i in primes ): primes.append(i)
#print primes
for i in primes:
if n%i == 0:
return [False,primes]
return [True,primes]
def factorize(n,primes=[2]):
factorization = {}
factor = 0
for i in primes:
if(n%i==0):
factor=i
break
if factor==0:
for i in xrange(primes[len(primes)-1],int(math.sqrt(n))+1):
ifprime = True
for j in primes:
if i%j==0:
ifprime = False
break
if ifprime:
if(n%i==0):
factor=i
break
if(not i in primes): primes.append(i)
if factor==0: return {n:1}
exp=0
while(n%factor==0):
exp+=1
n/=factor
factorization[factor]=exp
if(n!=factor and n!=1):
results=factorize(n)
for i in results.keys():
factorization[i]=results[i]
return factorization
def coprime_count(n,factors=[],primes=[2]):
if factors == []:
factors = factorize(n,primes=primes)
for i in factors.iterkeys():
n = int(n*(1-1.0/i))
return n
if __name__=="__main__":
#d = 12 # use "12" as the denominator for testing; should be 4/11
#print coprime_count(12)
d=2
res = 1
primes=[2]
# generate all primes from 2 to 1,000,000
'''for i in xrange(3,1000000,2):
#result = trialdiv(i,primes=primes)
#primes = result[1]
if(trialdiv(i)): primes.append(i)
print i
print "Done generating primes"
exit(0)'''
# continue generating primes
for i in xrange(2,24):
print i
ifprime = True
for j in primes:
if i%j == 0:
ifprime = False
break
if ifprime:
if( not i in primes ): primes.append(i)
'''for i in xrange(2,2819**2):
result = trialdiv(i)
if(result[0]): primes=result[1]'''
print "Done generating primes:", primes
ans = 1.0
num=1.0*3
for i in primes:
ans*=(1-1.0/i)
for i in primes:
num*=i
ans*=num/(num-1)
print num, ans
exit(1)
while(res>=15499.0/94744):
#if(d%10000==0): print d
res = float(coprime_count(d,primes=primes))/(d-1)
print d,res
d+=1
print d-1