From 671ab1d863b18edeb25d422926fe1521b6a71734 Mon Sep 17 00:00:00 2001 From: Peter Yao Date: Wed, 14 Oct 2020 20:43:39 -0700 Subject: [PATCH] Project Euler 62 Solution (#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 --- DIRECTORY.md | 2 + project_euler/problem_62/__init__.py | 0 project_euler/problem_62/sol1.py | 62 ++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 project_euler/problem_62/__init__.py create mode 100644 project_euler/problem_62/sol1.py diff --git a/DIRECTORY.md b/DIRECTORY.md index 8e7835e9c..0678e10bb 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -666,6 +666,8 @@ * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_551/sol1.py) * Problem 56 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_56/sol1.py) + * Problem 62 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_62/sol1.py) * Problem 63 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_63/sol1.py) * Problem 67 diff --git a/project_euler/problem_62/__init__.py b/project_euler/problem_62/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/project_euler/problem_62/sol1.py b/project_euler/problem_62/sol1.py new file mode 100644 index 000000000..83286c801 --- /dev/null +++ b/project_euler/problem_62/sol1.py @@ -0,0 +1,62 @@ +""" +Project Euler 62 +https://projecteuler.net/problem=62 + +The cube, 41063625 (345^3), can be permuted to produce two other cubes: +56623104 (384^3) and 66430125 (405^3). In fact, 41063625 is the smallest cube +which has exactly three permutations of its digits which are also cube. + +Find the smallest cube for which exactly five permutations of its digits are +cube. +""" + +from collections import defaultdict + + +def solution(max_base: int = 5) -> int: + """ + 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 + numbers that are the base of the cube. + + Once you find 5 numbers that produce the same sequence of digits, return + the smallest one, which is at index 0 since we insert each base number in + ascending order. + + >>> solution(2) + 125 + >>> solution(3) + 41063625 + """ + freqs = defaultdict(list) + num = 0 + + while True: + digits = get_digits(num) + freqs[digits].append(num) + + if len(freqs[digits]) == max_base: + base = freqs[digits][0] ** 3 + return base + + num += 1 + + +def get_digits(num: int) -> str: + """ + Computes the sorted sequence of digits of the cube of num. + + >>> get_digits(3) + '27' + >>> get_digits(99) + '027999' + >>> get_digits(123) + '0166788' + """ + return "".join(sorted(list(str(num ** 3)))) + + +if __name__ == "__main__": + print(f"{solution() = }")