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

Function solution

project_euler/problem_062/sol1.py:16–44  ·  view source on GitHub ↗

Iterate through every possible cube and sort the cube's digits in ascending order. Sorting maintains an ordering of the digits that allows you to compare permutations. Store each sorted sequence of digits in a dictionary, whose key is the sequence of digits and value is a list of

(max_base: int = 5)

Source from the content-addressed store, hash-verified

14
15
16def solution(max_base: int = 5) -> int:
17 """
18 Iterate through every possible cube and sort the cube's digits in
19 ascending order. Sorting maintains an ordering of the digits that allows
20 you to compare permutations. Store each sorted sequence of digits in a
21 dictionary, whose key is the sequence of digits and value is a list of
22 numbers that are the base of the cube.
23
24 Once you find 5 numbers that produce the same sequence of digits, return
25 the smallest one, which is at index 0 since we insert each base number in
26 ascending order.
27
28 >>> solution(2)
29 125
30 >>> solution(3)
31 41063625
32 """
33 freqs = defaultdict(list)
34 num = 0
35
36 while True:
37 digits = get_digits(num)
38 freqs[digits].append(num)
39
40 if len(freqs[digits]) == max_base:
41 base = freqs[digits][0] ** 3
42 return base
43
44 num += 1
45
46
47def get_digits(num: int) -> str:

Callers 1

sol1.pyFile · 0.70

Calls 2

get_digitsFunction · 0.85
appendMethod · 0.45

Tested by

no test coverage detected