MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / solution

Function solution

project_euler/problem_044/sol1.py:29–45  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

27
28
29def 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
48if __name__ == "__main__":

Callers 1

sol1.pyFile · 0.70

Calls 1

is_pentagonalFunction · 0.70

Tested by

no test coverage detected