Commit 13a15978 authored by Taddeus Kroes's avatar Taddeus Kroes

Coninued working on problem 243, making it more efficient

parent c80984d8
#!/usr/bin/env python
def is_resilient(n, d):
if n == 1:
return True
from __future__ import division
from math import sqrt, ceil
for div in xrange(2, min(n, d) + 1):
if not n % div and not d % div:
return False
def primes_until(n):
""" Sieve of Eratosthenes """
lst = [False] * n
i = 2
return True
while i < n:
if not lst[i]:
yield i
def resilience(d):
r = 0
for j in xrange(i, n, i):
lst[j] = True
i += 1
def times(a, b):
return a * b
def div(m, n):
return not divmod(n, m)[1]
MAX = 2000000
all_primes = list(primes_until(MAX))
for n in xrange(1, d):
if is_resilient(n, d):
r += 1
def primes(n):
for p in all_primes:
if p >= n:
raise StopIteration
return r / (d - 1.)
yield p
def phi(n):
return reduce(times, iter(1 - 1 / p for p in primes(n) if div(p, n)), n)
def resilience(d):
return phi(d) / (d - 1)
smallest = 15499. / 94744
d = 2
while True:
if resilience(d) < smallest:
print d
break
try:
while resilience(d) >= 15499 / 94744:
d += 1
if d == MAX:
print 'maximum reached:',
break
except KeyboardInterrupt:
print 'interrupted:',
d += 1
print d
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment