Returns the biggest prime, below the celing, that can be written as the sum of consecutive the most consecutive primes. >>> solution(500) 499 >>> solution(1_000) 953 >>> solution(10_000) 9521
(ceiling: int = 1_000_000)
| 52 | |
| 53 | |
| 54 | def solution(ceiling: int = 1_000_000) -> int: |
| 55 | """ |
| 56 | Returns the biggest prime, below the celing, that can be written as the sum |
| 57 | of consecutive the most consecutive primes. |
| 58 | |
| 59 | >>> solution(500) |
| 60 | 499 |
| 61 | |
| 62 | >>> solution(1_000) |
| 63 | 953 |
| 64 | |
| 65 | >>> solution(10_000) |
| 66 | 9521 |
| 67 | """ |
| 68 | primes = prime_sieve(ceiling) |
| 69 | length = 0 |
| 70 | largest = 0 |
| 71 | |
| 72 | for i in range(len(primes)): |
| 73 | for j in range(i + length, len(primes)): |
| 74 | sol = sum(primes[i:j]) |
| 75 | if sol >= ceiling: |
| 76 | break |
| 77 | |
| 78 | if sol in primes: |
| 79 | length = j - i |
| 80 | largest = sol |
| 81 | |
| 82 | return largest |
| 83 | |
| 84 | |
| 85 | if __name__ == "__main__": |