Skip to content

Commit 3c36849

Browse files
authored
Project Euler 62 Solution (TheAlgorithms#3029)
* Add solution for Project Euler 62 * Add doctests and annotate function params and return values for get_digits() * Add extra newline between functions to fix flake8 errors * Add extra newlines between function names * Add missing return type for solution() * Remove parenthesis from if statement * Remove parentheses from while loop * Add to explanation and fix second Travis build * Compress get_digits(), add tests for solution(), add fstring and positional arg for solution() * Remove input param when calling solution() * Remove test case for the answer
1 parent 2e5129d commit 3c36849

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

Diff for: DIRECTORY.md

+2
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,8 @@
666666
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_551/sol1.py)
667667
* Problem 56
668668
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_56/sol1.py)
669+
* Problem 62
670+
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_62/sol1.py)
669671
* Problem 63
670672
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_63/sol1.py)
671673
* Problem 67

Diff for: project_euler/problem_62/__init__.py

Whitespace-only changes.

Diff for: project_euler/problem_62/sol1.py

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
"""
2+
Project Euler 62
3+
https://projecteuler.net/problem=62
4+
5+
The cube, 41063625 (345^3), can be permuted to produce two other cubes:
6+
56623104 (384^3) and 66430125 (405^3). In fact, 41063625 is the smallest cube
7+
which has exactly three permutations of its digits which are also cube.
8+
9+
Find the smallest cube for which exactly five permutations of its digits are
10+
cube.
11+
"""
12+
13+
from collections import defaultdict
14+
15+
16+
def 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+
47+
def get_digits(num: int) -> str:
48+
"""
49+
Computes the sorted sequence of digits of the cube of num.
50+
51+
>>> get_digits(3)
52+
'27'
53+
>>> get_digits(99)
54+
'027999'
55+
>>> get_digits(123)
56+
'0166788'
57+
"""
58+
return "".join(sorted(list(str(num ** 3))))
59+
60+
61+
if __name__ == "__main__":
62+
print(f"{solution() = }")

0 commit comments

Comments
 (0)