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

Added solution for Project Euler problem 38. #3115

Merged
merged 5 commits into from
Oct 24, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Empty file.
74 changes: 74 additions & 0 deletions project_euler/problem_38/sol1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
"""
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 is_9_palindromic(n: int) -> bool:
"""
Checks whether n is a 9-digit 1 to 9 pandigital number.
>>> is_9_palindromic(12345)
False
>>> is_9_palindromic(156284973)
True
>>> is_9_palindromic(1562849733)
False
"""
s = str(n)
return len(s) == 9 and set(s) == set("123456789")


def solution() -> int:
"""
Return the largest 1 to 9 pandigital 9-digital number that can be formed as the
concatenated product of an integer with (1,2,...,n) where n > 1.

Solution:
Since n>1, the largest candidate for the solution will be a concactenation of
a 4-digit number and its double, a 5-digit number.
Let a be the 4-digit number.
a has 4 digits => 1000 <= a < 10000
2a has 5 digits => 10000 <= 2a < 100000
=> 5000 <= a < 10000

The concatenation of a with 2a = a * 10^5 + 2a
so our candidate for a given a is 100002 * a.
We iterate through the search space 5000 <= a < 10000 in reverse order,
calculating the candidates for each a and checking if they are 1-9 pandigital.

In case there are no 4-digit numbers that satisfy this property, we check
the 3-digit numbers with a similar formula (the example a=192 gives a lower
bound on the length of a):
a has 3 digits, etc...
=> 100 <= a < 334, candidate = a * 10^6 + 2a * 10^3 + 3a
= 1002003 * a

"""
for base_num in range(9999, 4999, -1):
candidate = 100002 * base_num
if is_9_palindromic(candidate):
return candidate

for base_num in range(333, 99, -1):
candidate = 1002003 * base_num
if is_9_palindromic(candidate):
return candidate

return 192384576


if __name__ == "__main__":
print(solution())