mp.append(sum(arr)) # print(mp) for i in range(len(mp)): # print(i, mp[i - 1], mp[i]) for j in range(mp[i - 1], mp[i]): out[j] = i # out[mp[len(mp) - 1]] = len(mp) - 1 return tuple([0] + out) pm = {2: p_to_m(2), 3: p_to_m(3), 5: p_to_m(5), 7: p_to_m(7)} for x in pm: print(x, pm[x], len(pm[x])) M = 10**8 pp = mylib.primes_from_file(M) f_cashed = dict() def get_f(dd): if dd in f_cashed: return f_cashed[dd] # print(' ', dd) d = dd[0] p = len(dd) if d in pm: p = pm[d][p] f_cashed[dd] = d * p return f_cashed[dd]
If N is admissible, the smallest integer M > 1 such that N+M is prime, will be called the pseudo-Fortunate number for N. For example, N=630 is admissible since it is even and its distinct prime factors are the consecutive primes 2,3,5 and 7. The next prime number after 631 is 641; hence, the pseudo-Fortunate number for 630 is M=11. It can also be seen that the pseudo-Fortunate number for 16 is 3. Find the sum of all distinct pseudo-Fortunate numbers for admissible numbers N less than 10**9. """ import math import mylib top = 10 ** 9 pp_100 = sorted(mylib.primes_from_file(100)) bb = [] m = 1 for x in pp_100: m *= x if m >= top: m //= x break bb.append(x) print(bb, m) rrr = [[1]] for b in bb: mm = sorted(rrr[len(rrr) - 1]) p = math.floor(math.log(top, b)) nn = [b ** (x + 1) for x in range(p)]
import mylib import math L = 20 r = math.floor(L ** 0.5) pp = sorted(x for x in mylib.primes_from_file(r) if x <= r) print(pp) for m in pp: print(m)
import mylib, math pp = sorted(mylib.primes_from_file(10 ** 6 + 1)) ends = dict((x, [x * y % 10 for y in range(10)]) for x in [1, 3, 7, 9]) # print(ends) ends_map = dict() for p in ends: ends_map[p] = [0 for x in range(10)] for i in range(len(ends[p])): ends_map[p][ends[p][i]] = i print(ends_map) def divide(x, y): x_str = str(x) z = x * 1 pwr = len(x_str) d = 0 e = y % 10 print('*' + str(x), '/', y, end=' ') for c in range(pwr): n = z % 10 m = ends_map[e][n] d += m * 10 ** c z = (z - m * y) // 10 print(' > ', n, m, m * y, end=' ') print(' : ', d * y, '/', y, '=', d) return d * y
mp.append(sum(arr)) # print(mp) for i in range(len(mp)): # print(i, mp[i - 1], mp[i]) for j in range(mp[i - 1], mp[i]): out[j] = i # out[mp[len(mp) - 1]] = len(mp) - 1 return tuple([0] + out) pm = {2: p_to_m(2), 3: p_to_m(3), 5: p_to_m(5), 7: p_to_m(7)} for x in pm: print(x, pm[x], len(pm[x])) M = 10 ** 8 pp = mylib.primes_from_file(M) f_cashed = dict() def get_f(dd): if dd in f_cashed: return f_cashed[dd] # print(' ', dd) d = dd[0] p = len(dd) if d in pm: p = pm[d][p] f_cashed[dd] = d * p return f_cashed[dd]
import mylib, math m = 500500507 t = 500500 pp = sorted(mylib.primes_from_file(10**7))[:t] p_max = max(pp) p_max_r = math.ceil(p_max**0.5) print(len(pp), p_max, p_max_r, p_max_r**2) rr = pp[:t] qq = [q for q in pp if q <= p_max_r] print(qq) for q in qq: x = q**2 while x < p_max: print(q, x) rr.append(x) x = x**2 rr = sorted(rr)[:t] print(max(rr)) s = 1 for i in range(len(rr)): r = rr[i] if not i % 10: print(i, r) s = (s * r) % m print(s)
def init_primes(p): mylib.known_primes = mylib.primes_from_file(10 ** math.ceil(p / 2)) print('primes:', '... ' + ', '.join([str(x) for x in (sorted(mylib.known_primes)[-10:])]))
import mylib e = 4 ppp = mylib.primes_from_file(10 ** (2 * e)) pp = sorted(p for p in ppp if p < 10 ** e) ss = [{}] max_len = 0 for p in pp: if p == 2: continue ss_add = [] for s in ss: if not len(s): ss_add.append({p}) continue all_primes = True for q in s: pq = [str(p), str(q)] if int("".join(pq)) not in ppp or int("".join(pq[::-1])) not in ppp: all_primes = False break if all_primes: s_new = s | {p} if len(s_new) >= max_len: max_len = len(s_new) print(s_new) ss_add.append(s_new) ss += ss_add print([(s, sum(s)) for s in ss if len(s) >= max_len])
import mylib, math m = 500500507 t = 500500 pp = sorted(mylib.primes_from_file(10 ** 7))[:t] p_max = max(pp) p_max_r = math.ceil(p_max ** 0.5) print(len(pp), p_max, p_max_r, p_max_r ** 2) rr = pp[:t] qq = [q for q in pp if q <= p_max_r] print(qq) for q in qq: x = q ** 2 while x < p_max: print(q, x) rr.append(x) x = x ** 2 rr = sorted(rr)[:t] print(max(rr)) s = 1 for i in range(len(rr)): r = rr[i] if not i % 10: print(i, r) s = (s * r) % m print(s)
""" It is possible to write ten as the sum of primes in exactly five different ways: 7 + 3 5 + 5 5 + 3 + 2 3 + 3 + 2 + 2 2 + 2 + 2 + 2 + 2 What is the first value which can be written as the sum of primes in over five thousand different ways? """ import mylib pp = sorted(mylib.primes_from_file(100)) def find_ways(number, max_a=0): ways = [] for a in pp: if a > number: break if max_a and a > max_a: break if number == a: ways.append([a]) continue append_ways = find_ways(number - a, a) for w in append_ways: ways.append([a] + w) return ways
def init_primes(p): mylib.known_primes = mylib.primes_from_file(10**math.ceil(p / 2)) print( 'primes:', '... ' + ', '.join([str(x) for x in (sorted(mylib.known_primes)[-10:])]))
import mylib import math L = 20 r = math.floor(L**0.5) pp = sorted(x for x in mylib.primes_from_file(r) if x <= r) print(pp) for m in pp: print(m)
import mylib e = 4 ppp = mylib.primes_from_file(10**(2 * e)) pp = sorted(p for p in ppp if p < 10**e) ss = [{}] max_len = 0 for p in pp: if p == 2: continue ss_add = [] for s in ss: if not len(s): ss_add.append({p}) continue all_primes = True for q in s: pq = [str(p), str(q)] if int(''.join(pq)) not in ppp or int(''.join( pq[::-1])) not in ppp: all_primes = False break if all_primes: s_new = s | {p} if len(s_new) >= max_len: max_len = len(s_new) print(s_new) ss_add.append(s_new) ss += ss_add print([(s, sum(s)) for s in ss if len(s) >= max_len])
import mylib, math pp = sorted(mylib.primes_from_file(10**6 + 1)) ends = dict((x, [x * y % 10 for y in range(10)]) for x in [1, 3, 7, 9]) # print(ends) ends_map = dict() for p in ends: ends_map[p] = [0 for x in range(10)] for i in range(len(ends[p])): ends_map[p][ends[p][i]] = i print(ends_map) def divide(x, y): x_str = str(x) z = x * 1 pwr = len(x_str) d = 0 e = y % 10 print('*' + str(x), '/', y, end=' ') for c in range(pwr): n = z % 10 m = ends_map[e][n] d += m * 10**c z = (z - m * y) // 10 print(' > ', n, m, m * y, end=' ') print(' : ', d * y, '/', y, '=', d) return d * y