Created
March 16, 2014 16:27
-
-
Save mtayseer/9585854 to your computer and use it in GitHub Desktop.
Solve project Euler problem #38 http://projecteuler.net/problem=38
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Analysis: | |
# | |
# Let's try to find the optimal range. We know that | |
# 1. 918273645 is a 1-9 pandigital & is a concatenated product, which means that this number is either the largest or | |
# below it, so we search from this number up | |
# 2. Then we find that the number we're trying to search for should start with 9, e.g. 9, 91, 945, etc | |
# 3. len(concat_product(x=90, n=3)) = 8, len(concat_product(x=90, n=4)) = 11. This range won't work. | |
# 4. len(concat_product(x=9000, n=2)) = 9. This is where we should start. | |
# 5. Taking #1 in consideration, we will start from 9182 till 9999 | |
# 6. To get the pandigital number from a this range, we can multiply by n * 100000 + n * 2 => n * 100002 | |
# 7. We get the largest x fitting our definition by walking backwards | |
from time import time | |
before = time() | |
concatenated_products = (x * 100002 for x in xrange(9999, 9181, -1)) | |
digits = set('123456789') | |
pandigitals = (p for p in concatenated_products if set(str(p)) == digits) | |
print next(pandigitals) | |
print time() - before |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment