problem 44 https projecteuler netproblem44 pentagonal numbers are generated by the formula pnn3n12 the first ten pentagonal numbers are 1 5 12 22 35 51 70 92 117 145 it can be seen that p4 p7 22 70 92 p8 however their difference 70 22 48 is not pentagonal find the pair of pentagonal numbers pj and pk for which their sum and difference are pentagonal and d pk pj is minimised what is the value of d returns true if n is pentagonal false otherwise ispentagonal330 true ispentagonal7683 false ispentagonal2380 true returns the minimum difference of two pentagonal numbers p1 and p2 such that p1 p2 is pentagonal and p2 p1 is pentagonal solution5000 5482660 returns true if n is pentagonal false otherwise is_pentagonal 330 true is_pentagonal 7683 false is_pentagonal 2380 true 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
def is_pentagonal(n: int) -> bool:
    
    root = (1 + 24 * n) ** 0.5
    return ((1 + root) / 6) % 1 == 0


def solution(limit: int = 5000) -> int:
    
    pentagonal_nums = [(i * (3 * i - 1)) // 2 for i in range(1, limit)]
    for i, pentagonal_i in enumerate(pentagonal_nums):
        for j in range(i, len(pentagonal_nums)):
            pentagonal_j = pentagonal_nums[j]
            a = pentagonal_i + pentagonal_j
            b = pentagonal_j - pentagonal_i
            if is_pentagonal(a) and is_pentagonal(b):
                return b

    return -1


if __name__ == "__main__":
    print(f"{solution() = }")