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

Function solution

project_euler/problem_058/sol1.py:79–99  ·  view source on GitHub ↗

Returns the side length of the square spiral of odd length greater than 1 for which the ratio of primes along both diagonals first falls below the given ratio. >>> solution(.5) 11 >>> solution(.2) 309 >>> solution(.111) 11317

(ratio: float = 0.1)

Source from the content-addressed store, hash-verified

77
78
79def solution(ratio: float = 0.1) -> int:
80 """
81 Returns the side length of the square spiral of odd length greater
82 than 1 for which the ratio of primes along both diagonals
83 first falls below the given ratio.
84 >>> solution(.5)
85 11
86 >>> solution(.2)
87 309
88 >>> solution(.111)
89 11317
90 """
91
92 j = 3
93 primes = 3
94
95 while primes / (2 * j - 1) >= ratio:
96 for i in range(j * j + j + 1, (j + 2) * (j + 2), j + 1):
97 primes += is_prime(i)
98 j += 2
99 return j
100
101
102if __name__ == "__main__":

Callers

nothing calls this directly

Calls 1

is_primeFunction · 0.70

Tested by

no test coverage detected