Returns the number of distinct terms in the sequence generated by a^b for 2 <= a <= 100 and 2 <= b <= 100. >>> solution(100) 9183 >>> solution(50) 2184 >>> solution(20) 324 >>> solution(5) 15 >>> solution(2) 1 >>> solution(1) 0
(n: int = 100)
| 17 | |
| 18 | |
| 19 | def solution(n: int = 100) -> int: |
| 20 | """Returns the number of distinct terms in the sequence generated by a^b |
| 21 | for 2 <= a <= 100 and 2 <= b <= 100. |
| 22 | |
| 23 | >>> solution(100) |
| 24 | 9183 |
| 25 | >>> solution(50) |
| 26 | 2184 |
| 27 | >>> solution(20) |
| 28 | 324 |
| 29 | >>> solution(5) |
| 30 | 15 |
| 31 | >>> solution(2) |
| 32 | 1 |
| 33 | >>> solution(1) |
| 34 | 0 |
| 35 | """ |
| 36 | collect_powers = set() |
| 37 | |
| 38 | current_pow = 0 |
| 39 | |
| 40 | n = n + 1 # maximum limit |
| 41 | |
| 42 | for a in range(2, n): |
| 43 | for b in range(2, n): |
| 44 | current_pow = a**b # calculates the current power |
| 45 | collect_powers.add(current_pow) # adds the result to the set |
| 46 | return len(collect_powers) |
| 47 | |
| 48 | |
| 49 | if __name__ == "__main__": |