#In fact, with the exception of p1 = 3 and p2 = 5, for every pair of consecutive primes, p2 > p1, there exist values of n for which the last digits are formed by p1 and n is divisible by p2. Let S be the smallest of these values of n. #Find ∑ S for every pair of consecutive primes with 5 ≤ p1 ≤ 1000000. #Answer: #18613426663617118 #https://github.com/skydark/project-euler/blob/master/134.py from time import time; t=time() from mathplus import log10, get_primes_by_sieve M = 10**6+5 primes, _ = get_primes_by_sieve(M) def f(n, m): #assert n > m if (n, m) == (1, 0): return 1, 0 q, r = n//m, n%m a, b = f(m, r) return b, a-b*q s = 0 for i in range(2, len(primes)-1): p, q = primes[i:i+2] n = 10**(int(log10(p))+1) a, b = f(q, n%q) s += (b*(q-p) % q)*n+p print(s)#, time()-t
def concat(i, j): return 10**(int(log10(j))+1)*i+j
#Find ∑ S for every pair of consecutive primes with 5 ≤ p1 ≤ 1000000. #Answer: #18613426663617118 #https://github.com/skydark/project-euler/blob/master/134.py from time import time t = time() from mathplus import log10, get_primes_by_sieve M = 10**6 + 5 primes, _ = get_primes_by_sieve(M) def f(n, m): #assert n > m if (n, m) == (1, 0): return 1, 0 q, r = n // m, n % m a, b = f(m, r) return b, a - b * q s = 0 for i in range(2, len(primes) - 1): p, q = primes[i:i + 2] n = 10**(int(log10(p)) + 1) a, b = f(q, n % q) s += (b * (q - p) % q) * n + p print(s) #, time()-t
#So the first few Hamming numbers are 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15. #There are 1105 Hamming numbers not exceeding 108. #We will call a positive number a generalised Hamming number of type n, if it has no prime factor larger than n. #Hence the Hamming numbers are the generalised Hamming numbers of type 5. #How many generalised Hamming numbers of type 100 are there which don't exceed 109? #Answer: #2944730 from time import time; t=time() from mathplus import log10 M = 9 P = [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97] pl = [log10(p) for p in P] def f(pos, limit): if pos == 0: return int(limit/pl[0])+1 v = pl[pos] pos -= 1 s = 0 while limit > 0: s += f(pos, limit) limit -= v return s print(f(len(pl)-1, M))#, time()-t