def nb2(lim): # n = p^3*q with n <= lim s = 0 for i in range(len(l_p)): # q < p p = l_p[i] if p**3 > lim // 2: break s += min(binary_search_inf(l_p[:i + 1], lim / (p**3)) + 1, i) for i in range(len(l_p)): # q > p q = l_p[i] b = lim / q if b < 8: break s += min(binary_search_inf(l_p[:i + 1], b**(1 / 3)) + 1, i) return s
def problem_49(): l4 = numpy_sieve(10**4) i0 = binary_search_inf(l4, 1000) l4 = l4[i0 + 1:] def permut_ok(n): m2 = str(n) m = list(m2) m.sort() for i in range(1, 3): n2 = n + i * 3330 if not binary_search(l4, n2)[0]: return False, "" n2 = str(n2) nn2 = list(n2) nn2.sort() if nn2 != m: return False, "" m2 += n2 return True, m2 for p in l4: if p != 1487: ok, s = permut_ok(p) if ok: print(s) # sol = 296962999629 break
def nb1(lim): # n = p^7 with n <= lim a = int(lim**(1 / 7)) if (a + 1)**7 < lim: a += 1 if a < 2: return 0 return binary_search_inf(l_p, a) + 1
def nb3(lim): s = 0 for p in l_p: # p > q > r for i in range(len(l_p)): q = l_p[i] if q >= p: break if q * p * 2 > lim: break s += min(binary_search_inf(l_p[:i + 1], lim / (p * q)) + 1, i) return s
def problem_51(): l_7 = numpy_sieve(10**7) i_start = binary_search_inf(l_7, 10**5) def make_pattern(n): m = str(n) for digit in m: if m.count(digit) == 3: yield [ 5 - i for i in range(len(m) - 1, -1, -1) if m[i] == digit ] def apply_pattern(n, pat): n2 = 0 k = 0 while n != 0: n2 += (n % 10) * 10**k if k not in pat else 0 n //= 10 k += 1 return n2 def add(n, pat): for i in pat: n += 10**i return n viewed = set() for p in l_7[i_start:]: for pattern in make_pattern(p): p2 = apply_pattern(p, pattern) if p2 in viewed: continue viewed.add(p2) nb = 0 for d in range(10): if (5 not in pattern or d != 0) and binary_search(l_7, p2)[0]: nb += 1 if nb + 9 - d < 8: break p2 = add(p2, pattern) if nb < 8: continue print(p) # sol = 121313 return
elif n % 4 == 3: k = n p = lpd(k) if p == 0 or p == k: return True return ok_aux(p, k) else: return False def number(n): nb = 0 for i in range(n): if ok(i): nb += 1 return nb print("Problem 136: ", number(10**6*50)) # the 'good' n's are n == 4P or 16P or P where P prime and n == 4*1 and n == 16*1 # so : maxi = 10**6*50 l_pp = numpy_sieve(maxi) tot = 0 for a in l_pp: if a % 4 == 3: tot += 1 tot += binary_search_inf(l_pp, maxi//4) + 1 tot += binary_search_inf(l_pp, maxi//16) + 1 print(tot)
def ok(p): pp = str(p) for c in pp: if c == '0': return False if pp.count(c) > 1: return False return True l8bis = [prime for prime in l_8 if ok(prime)] primes = [[]] i = 0 k0 = 1 while k0 <= 8: j = binary_search_inf(l8bis, 10**k0) primes.append(l8bis[i:j + 1]) i = j + 1 k0 += 1 def _in(p): p = str(p) ll = list(p) ll.sort() return "".join(ll) dict_in = [{} for _2 in range(10)] for prime in l8bis: nb_d = len(str(prime))
for i9 in range(i8 + 1, len_lp): p9 = lp[i9] if p2 * p1 * p3 * p4 * p5 * p6 * p7 * p8 * p9 > l: break lp2.append(p2 * p1 * p3 * p4 * p5 * p6 * p7 * p8 * p9) lp2.sort() l2 = [] am = log(l) / log(2) for a in range(int(am) + 1): if a >= 32: break bm = (log(l) - a * log(2)) / log(3) for b in range(int(bm) + 1): cm = (log(l) - a * log(2) - b * log(3)) / log(5) for c in range(int(cm) + 1): n = 2**a * 3**b * 5**c if n > l: break l2.append(n) l2.sort() s = sum(l2) % 2**32 for p in lp2: k = binary_search_inf(l2, 10**12 / p) s += p * sum(l2[:k + 1]) s %= 2**32 print(s) # 939087315