MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / solution

Function solution

project_euler/problem_050/sol1.py:54–82  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

52
53
54def 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
85if __name__ == "__main__":

Callers 1

sol1.pyFile · 0.70

Calls 1

prime_sieveFunction · 0.70

Tested by

no test coverage detected