def f(n, m, p): # assert p in primes return pow_mod(n, m % (p-1), p)
#!/usr/bin/python # -*- coding: utf-8 -*- #The first known prime found to exceed one million digits was discovered in 1999, and is a Mersenne prime of the form 26972593−1; it contains exactly 2,098,960 digits. Subsequently other Mersenne primes, of the form 2p−1, have been found which contain more digits. #However, in 2004 there was found a massive non-Mersenne prime which contains 2,357,207 digits: 28433×27830457+1. #Find the last ten digits of this prime number. #Answer: #8739992577 from time import time; t=time() from mathplus import pow_mod M = 10**10 print((28433*pow_mod(2, 7830457, M)) % M + 1)#, time()-t
def report(p): print(p)#, time()-t) import sys sys.exit() for p in range(S, T, 2): if p % 5 == 0: continue phip = phi(p) m = p if p % 3 == 0: phip *= 9 m *= 9 if phip <= M: continue factors = factorization(phip) while phip > M: for f, c in factors: phif = phip//f if pow_mod(10, phif, m) == 1: phip = phif if c > 1: factors = [(k, v) if k != f else (k, v-1) for k, v in factors] else: factors = [(k, v) for k, v in factors if k != f] break else: report(p) print('Failed!', time()-t)
#!/usr/bin/python # -*- coding: utf-8 -*- #The series, 11 + 22 + 33 + ... + 1010 = 10405071317. #Find the last ten digits of the series, 11 + 22 + 33 + ... + 10001000. #Answer: #9110846700 from mathplus import pow_mod M = 1000 L = 10 p = 10**L print(sum(pow_mod(i, i, p) for i in range(1, M)) % p)
#Although R(10), R(100), or R(1000) are not divisible by 17, R(10000) is divisible by 17. Yet there is no value of n for which R(10n) will divide by 19. In fact, it is remarkable that 11, 17, 41, and 73 are the only four primes below one-hundred that can be a factor of R(10n). #Find the sum of all the primes below one-hundred thousand that will never be a factor of R(10n). #Answer: #453647705 from time import time t = time() from mathplus import get_phis_by_sieve, pow_mod M = 100000 phis = get_phis_by_sieve(M) s = 3 for p, phip in enumerate(phis): if phip != p - 1 or p == 1: continue k = 1 while phip % 2 == 0: phip //= 2 k *= 2 while phip % 5 == 0: phip //= 5 k *= 5 if pow_mod(10, k, p) != 1: s += p print(s) #, time()-t)
def f(a, b, p): if b == 1 or p == 2: return a % p x = f(a, b-1, phi(p)) return pow_mod(a, x, p)
#Let us consider repunits of the form R(10n). #Although R(10), R(100), or R(1000) are not divisible by 17, R(10000) is divisible by 17. Yet there is no value of n for which R(10n) will divide by 19. In fact, it is remarkable that 11, 17, 41, and 73 are the only four primes below one-hundred that can be a factor of R(10n). #Find the sum of all the primes below one-hundred thousand that will never be a factor of R(10n). #Answer: #453647705 from time import time; t=time() from mathplus import get_phis_by_sieve, pow_mod M = 100000 phis = get_phis_by_sieve(M) s = 3 for p, phip in enumerate(phis): if phip != p - 1 or p == 1: continue k = 1 while phip % 2 == 0: phip //= 2 k *= 2 while phip % 5 == 0: phip //= 5 k *= 5 if pow_mod(10, k, p) != 1: s += p print(s)#, time()-t)
def f(a, b, p): if b == 1 or p == 2: return a % p x = f(a, b - 1, phi(p)) return pow_mod(a, x, p)
#You are given that for all primes, p > 5, that p − 1 is divisible by A(p). For example, when p = 41, A(41) = 5, and 40 is divisible by 5. #However, there are rare composite values for which this is also true; the first five examples being 91, 259, 451, 481, and 703. #Find the sum of the first twenty-five composite values of n for which #GCD(n, 10) = 1 and n − 1 is divisible by A(n). #Answer: #149253 from time import time; t=time() from mathplus import pow_mod, get_primes_by_sieve M = 10**6 C = 25 L = [] primes, sieves = get_primes_by_sieve(M) n, c = 7, C while True: n += 2 if n % 3 == 0 or n % 5 == 0 or sieves[n]: continue if pow_mod(10, n-1, n) == 1: c -= 1 L.append(n) if c == 0: break print(sum(L))#, time()-t
def f(n, m, p): # assert p in primes return pow_mod(n, m % (p - 1), p)
#!/usr/bin/python # -*- coding: utf-8 -*- # The series, 11 + 22 + 33 + ... + 1010 = 10405071317. # Find the last ten digits of the series, 11 + 22 + 33 + ... + 10001000. # Answer: # 9110846700 from mathplus import pow_mod M = 1000 L = 10 p = 10 ** L print(sum(pow_mod(i, i, p) for i in range(1, M)) % p)
#!/usr/bin/python # -*- coding: utf-8 -*- #The first known prime found to exceed one million digits was discovered in 1999, and is a Mersenne prime of the form 26972593−1; it contains exactly 2,098,960 digits. Subsequently other Mersenne primes, of the form 2p−1, have been found which contain more digits. #However, in 2004 there was found a massive non-Mersenne prime which contains 2,357,207 digits: 28433×27830457+1. #Find the last ten digits of this prime number. #Answer: #8739992577 from time import time t = time() from mathplus import pow_mod M = 10**10 print((28433 * pow_mod(2, 7830457, M)) % M + 1) #, time()-t
#You are given that for all primes, p > 5, that p − 1 is divisible by A(p). For example, when p = 41, A(41) = 5, and 40 is divisible by 5. #However, there are rare composite values for which this is also true; the first five examples being 91, 259, 451, 481, and 703. #Find the sum of the first twenty-five composite values of n for which #GCD(n, 10) = 1 and n − 1 is divisible by A(n). #Answer: #149253 from time import time t = time() from mathplus import pow_mod, get_primes_by_sieve M = 10**6 C = 25 L = [] primes, sieves = get_primes_by_sieve(M) n, c = 7, C while True: n += 2 if n % 3 == 0 or n % 5 == 0 or sieves[n]: continue if pow_mod(10, n - 1, n) == 1: c -= 1 L.append(n) if c == 0: break print(sum(L)) #, time()-t