Resilience Problem 243 A positive fraction whose numerator is less than its denominator is called a proper fraction. For any denominator, d, there will be d−1 proper fractions; for example, with d = 12: 1/12 , 2/12 , 3/12 , 4/12 , 5/12 , 6/12 , 7/12 , 8/12 , 9/12 , 10/12 , 11/12 . We shall call a fraction that cannot be cancelled down a resilient fraction. Furthermore we shall define the resilience of a denominator, R(d), to be the ratio of its proper fractions that are resilient; for example, R(12) = 4/11 . In fact, d = 12 is the smallest denominator having a resilience R(d) < 4/10 . Find the smallest denominator d, having a resilience R(d) < 15499/94744 . """ from pe069 import prime_sieve __date__ = '14-4-19' __author__ = 'SUN' if __name__ == '__main__': prime = prime_sieve(30) n = 1 d = 1 for i in range(len(prime)): n *= prime[i] - 1 d *= prime[i] for j in range(2, prime[i]): if n * j / (d * j - 1) < 15499 / 94744: print(d * j) exit(0)
""" Prime square remainders Problem 123 Let pn be the nth prime: 2, 3, 5, 7, 11, ..., and let r be the remainder when (pn−1)^n + (pn+1)^n is divided by pn^2. For example, when n = 3, p3 = 5, and 4^3 + 6^3 = 280 ≡ 5 mod 25. The least value of n for which the remainder first exceeds 10^9 is 7037. Find the least value of n for which the remainder first exceeds 10^10. """ import time from pe069 import prime_sieve __date__ = '14-4-24' __author__ = 'SUN' if __name__ == '__main__': start = time.clock() prime = prime_sieve(300000) for i in range(0, len(prime), 2): if 2 * (i + 1) * prime[i] % prime[i] ** 2 > 10000000000: print(i + 1) break print('Runtime is', time.clock() - start)
""" Prime square remainders Problem 123 Let pn be the nth prime: 2, 3, 5, 7, 11, ..., and let r be the remainder when (pn−1)^n + (pn+1)^n is divided by pn^2. For example, when n = 3, p3 = 5, and 4^3 + 6^3 = 280 ≡ 5 mod 25. The least value of n for which the remainder first exceeds 10^9 is 7037. Find the least value of n for which the remainder first exceeds 10^10. """ import time from pe069 import prime_sieve __date__ = '14-4-24' __author__ = 'SUN' if __name__ == '__main__': start = time.clock() prime = prime_sieve(300000) for i in range(0, len(prime), 2): if 2 * (i + 1) * prime[i] % prime[i]**2 > 10000000000: print(i + 1) break print('Runtime is', time.clock() - start)
''' Summation of primes Problem 10 The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million. ''' import time from pe069 import prime_sieve __author__ = 'SUN' if __name__ == '__main__': start = time.clock() print(sum(prime_sieve(2000000))) print('Runtime is', time.clock() - start)
phi = phi // i * (i - 1) if n > 1: phi *= n - 1 return phi def totient_array(n): array = [x for x in range(n + 1)] for i in range(2, n + 1): if array[i] == i: for j in range(i, n + 1, i): array[j] = array[j] // i * (i - 1) return array if __name__ == '__main__': start = time.clock() N = 10000000 prime = prime_sieve(4000) answer = 0 ratio = 3 for i in range(len(prime)): for j in range(i, len(prime)): phi = prime[i] * prime[j] if phi > N: break if sorted(str((prime[i] - 1) * (prime[j] - 1))) == sorted(str(phi)) \ and ratio > phi / ((prime[i] - 1) * (prime[j] - 1)): answer = phi ratio = phi / ((prime[i] - 1) * (prime[j] - 1)) print(answer) print('Runtime is ', time.clock() - start)
''' 10001st prime Problem 7 By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. What is the 10 001st prime number? ''' from pe069 import prime_sieve __author__ = 'SUN' if __name__ == '__main__': print(prime_sieve(150000)[10000])
if n > 1: phi *= n - 1 return phi def totient_array(n): array = [x for x in range(n + 1)] for i in range(2, n + 1): if array[i] == i: for j in range(i, n + 1, i): array[j] = array[j] // i * (i - 1) return array if __name__ == '__main__': start = time.clock() N = 10000000 prime = prime_sieve(4000) answer = 0 ratio = 3 for i in range(len(prime)): for j in range(i, len(prime)): phi = prime[i] * prime[j] if phi > N: break if sorted(str((prime[i] - 1) * (prime[j] - 1))) == sorted(str(phi)) \ and ratio > phi / ((prime[i] - 1) * (prime[j] - 1)): answer = phi ratio = phi / ((prime[i] - 1) * (prime[j] - 1)) print(answer) print('Runtime is ', time.clock() - start)