Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add solution for Project Euler: Problem 38 #2935

Open
wants to merge 3 commits into
base: master
from
Open
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

@@ -28,6 +28,7 @@
* [Binary And Operator](https://github.com/TheAlgorithms/Python/blob/master/bit_manipulation/binary_and_operator.py)
* [Binary Or Operator](https://github.com/TheAlgorithms/Python/blob/master/bit_manipulation/binary_or_operator.py)
* [Binary Xor Operator](https://github.com/TheAlgorithms/Python/blob/master/bit_manipulation/binary_xor_operator.py)
* [Single Bit Manipulation Operations](https://github.com/TheAlgorithms/Python/blob/master/bit_manipulation/single_bit_manipulation_operations.py)

## Blockchain
* [Chinese Remainder Theorem](https://github.com/TheAlgorithms/Python/blob/master/blockchain/chinese_remainder_theorem.py)
@@ -265,6 +266,7 @@
* [Basic Graphs](https://github.com/TheAlgorithms/Python/blob/master/graphs/basic_graphs.py)
* [Bellman Ford](https://github.com/TheAlgorithms/Python/blob/master/graphs/bellman_ford.py)
* [Bfs Shortest Path](https://github.com/TheAlgorithms/Python/blob/master/graphs/bfs_shortest_path.py)
* [Bfs Zero One Shortest Path](https://github.com/TheAlgorithms/Python/blob/master/graphs/bfs_zero_one_shortest_path.py)
* [Bidirectional A Star](https://github.com/TheAlgorithms/Python/blob/master/graphs/bidirectional_a_star.py)
* [Bidirectional Breadth First Search](https://github.com/TheAlgorithms/Python/blob/master/graphs/bidirectional_breadth_first_search.py)
* [Breadth First Search](https://github.com/TheAlgorithms/Python/blob/master/graphs/breadth_first_search.py)
@@ -634,6 +636,7 @@
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_037/sol1.py)
* Problem 038
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_038/sol1.py)
* [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_038/sol2.py)
* Problem 039
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_039/sol1.py)
* Problem 040
@@ -694,6 +697,8 @@
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_076/sol1.py)
* Problem 080
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_080/sol1.py)
* Problem 081
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_081/sol1.py)
* Problem 091
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_091/sol1.py)
* Problem 097
@@ -0,0 +1,79 @@
"""
Project Euler Problem 38: https://projecteuler.net/problem=38
Take the number 192 and multiply it by each of 1, 2, and 3:
192 × 1 = 192
192 × 2 = 384
192 × 3 = 576
By concatenating each product we get the 1 to 9 pandigital, 192384576. We will
call 192384576 the concatenated product of 192 and (1,2,3)
The same can be achieved by starting with 9 and multiplying by 1, 2, 3, 4, and 5,
giving the pandigital, 918273645, which is the concatenated product of 9 and
(1,2,3,4,5).
What is the largest 1 to 9 pandigital 9-digit number that can be formed as the
concatenated product of an integer with (1,2, ... , n) where n > 1?
"""


def solution() -> int:
"""
Calculates and returns the largest 1-9 pandigital 9-digit number as product of
integer set n>1.
>>> solution()
932718654
Assumptions:
1. Since n>1, we'll concatenate the fixed number at least twice. We only need
9 digits, so 2x5=10 would exceed this safely. Thus target number is 4
digits or less.
2. We know at least one 9-digit pandigital starts with 9 as per the example,
so can short circuit search based on that.
3. We can start with 90, the next 9-start number following 9 (already calculated)
"""

largest_pandigital = 918273645

for fixed_num in range(90, 10000): # assumption 1 and 3
if not str(fixed_num).startswith("9"): # assumption 2
continue

pan = fixed_num
i = 2
while len(str(pan)) < 9:
pan = int(str(pan) + str(fixed_num * i))

if is_pandigital(pan) and pan > largest_pandigital:
largest_pandigital = pan

return largest_pandigital


def is_pandigital(num: int) -> bool:
"""
Validates that a number is 9-digit pandigital.
i.e. contains all numbers 1-9 without zero.
Examples:
>>> is_pandigital(123456789)
True
>>> is_pandigital(987654321)
True
>>> is_pandigital(1234567890)
False
>>> is_pandigital(111111111)
False
"""

# check that only digits 1-9 are present with no duplicates
numberset = set(str(num))
return len(numberset) == 9 and "0" not in numberset


if __name__ == "__main__":

print(f"{solution() = }")
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.