from tools import number_theory def is_prime(num): if not num % 2: return False for p in range(3, int(num ** 0.5) + 1, 2): if not num % p: return False return True def longest_slice(limit, prime): for l in range(len(prime)): if sum(prime[:l]) > limit: return (l - 1, sum(prime[:l - 1])) def reduce_slice(limit, prime, start, length, tale): tale -= prime[start] start += 1 t = tale while True: t = tale - prime[start] + prime[start + length] if t > limit: break tale = t return (start, tale) def shift_slice(limit, prime, start, length, tale): for s in range(start - 1, -1, -1): tale = tale + prime[s] - prime[s + length] if is_prime(tale): return (tale, length) def search(limit): prime = list(number_theory.make_prime(4000)) length, tale = longest_slice(limit, prime) if is_prime(tale): return (tale, length) start = 0 while length > 1: length -= 1 start, tale = reduce_slice(limit, prime, start, length, tale) if is_prime(tale): return (tale, length) get = shift_slice(limit, prime, start, length, tale) if get: return get print(search(1000000))