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)
| 77 | |
| 78 | |
| 79 | def 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 | |
| 102 | if __name__ == "__main__": |
nothing calls this directly
no test coverage detected