Skip to content
Snippets Groups Projects
Commit 13a15978 authored by Taddeus Kroes's avatar Taddeus Kroes
Browse files

Coninued working on problem 243, making it more efficient

parent c80984d8
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env python #!/usr/bin/env python
def is_resilient(n, d): from __future__ import division
if n == 1: from math import sqrt, ceil
return True
for div in xrange(2, min(n, d) + 1): def primes_until(n):
if not n % div and not d % div: """ Sieve of Eratosthenes """
return False lst = [False] * n
i = 2
return True while i < n:
if not lst[i]:
yield i
def resilience(d): for j in xrange(i, n, i):
r = 0 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): def primes(n):
if is_resilient(n, d): for p in all_primes:
r += 1 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 d = 2
while True: try:
if resilience(d) < smallest: while resilience(d) >= 15499 / 94744:
print d d += 1
break
if d == MAX:
print 'maximum reached:',
break
except KeyboardInterrupt:
print 'interrupted:',
d += 1 print d
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment