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

Function solution

project_euler/problem_068/sol1.py:46–77  ·  view source on GitHub ↗

Find the maximum number for a "magic" gon_side-gon ring The gon_side parameter should be in the range [3, 5], other side numbers aren't tested >>> solution(3) 432621513 >>> solution(4) 426561813732 >>> solution() 6531031914842725 >>> solution(6) Traceba

(gon_side: int = 5)

Source from the content-addressed store, hash-verified

44
45
46def solution(gon_side: int = 5) -> int:
47 """
48 Find the maximum number for a "magic" gon_side-gon ring
49
50 The gon_side parameter should be in the range [3, 5],
51 other side numbers aren't tested
52
53 >>> solution(3)
54 432621513
55 >>> solution(4)
56 426561813732
57 >>> solution()
58 6531031914842725
59 >>> solution(6)
60 Traceback (most recent call last):
61 ValueError: gon_side must be in the range [3, 5]
62 """
63 if gon_side < 3 or gon_side > 5:
64 raise ValueError("gon_side must be in the range [3, 5]")
65
66 # Since it's 16, we know 10 is on the outer ring
67 # Put the big numbers at the end so that they are never the first number
68 small_numbers = list(range(gon_side + 1, 0, -1))
69 big_numbers = list(range(gon_side + 2, gon_side * 2 + 1))
70
71 for perm in permutations(small_numbers + big_numbers):
72 numbers = generate_gon_ring(gon_side, list(perm))
73 if is_magic_gon(numbers):
74 return int("".join(str(n) for n in numbers))
75
76 msg = f"Magic {gon_side}-gon ring is impossible"
77 raise ValueError(msg)
78
79
80def generate_gon_ring(gon_side: int, perm: list[int]) -> list[int]:

Callers 1

sol1.pyFile · 0.70

Calls 2

generate_gon_ringFunction · 0.85
is_magic_gonFunction · 0.85

Tested by

no test coverage detected