Returns the minimum difference of two pentagonal numbers P1 and P2 such that P1 + P2 is pentagonal and P2 - P1 is pentagonal. >>> solution(5000) 5482660
(limit: int = 5000)
| 27 | |
| 28 | |
| 29 | def solution(limit: int = 5000) -> int: |
| 30 | """ |
| 31 | Returns the minimum difference of two pentagonal numbers P1 and P2 such that |
| 32 | P1 + P2 is pentagonal and P2 - P1 is pentagonal. |
| 33 | >>> solution(5000) |
| 34 | 5482660 |
| 35 | """ |
| 36 | pentagonal_nums = [(i * (3 * i - 1)) // 2 for i in range(1, limit)] |
| 37 | for i, pentagonal_i in enumerate(pentagonal_nums): |
| 38 | for j in range(i, len(pentagonal_nums)): |
| 39 | pentagonal_j = pentagonal_nums[j] |
| 40 | a = pentagonal_i + pentagonal_j |
| 41 | b = pentagonal_j - pentagonal_i |
| 42 | if is_pentagonal(a) and is_pentagonal(b): |
| 43 | return b |
| 44 | |
| 45 | return -1 |
| 46 | |
| 47 | |
| 48 | if __name__ == "__main__": |