Finds the value of n from 1 to max such that n/φ(n) produces a minimum. >>> solution(100) 21 >>> solution(10000) 4435
(max_n: int = 10000000)
| 73 | |
| 74 | |
| 75 | def solution(max_n: int = 10000000) -> int: |
| 76 | """ |
| 77 | Finds the value of n from 1 to max such that n/φ(n) produces a minimum. |
| 78 | |
| 79 | >>> solution(100) |
| 80 | 21 |
| 81 | |
| 82 | >>> solution(10000) |
| 83 | 4435 |
| 84 | """ |
| 85 | |
| 86 | min_numerator = 1 # i |
| 87 | min_denominator = 0 # φ(i) |
| 88 | totients = get_totients(max_n + 1) |
| 89 | |
| 90 | for i in range(2, max_n + 1): |
| 91 | t = totients[i] |
| 92 | |
| 93 | if i * min_denominator < min_numerator * t and has_same_digits(i, t): |
| 94 | min_numerator = i |
| 95 | min_denominator = t |
| 96 | |
| 97 | return min_numerator |
| 98 | |
| 99 | |
| 100 | if __name__ == "__main__": |
no test coverage detected