Preface:
I'm NOT a professional programmer nor an undergraduate in computer science or any kind of that. I'm simply a guy who's trying to learn how to code.
After binging some Youtube playlists for python, I've decided to challenge myself with Project Euler, and this series of blogs is my attempt at solving this problems "the dumb way", meaning using brute force and not some mathematical formula to find the solution.
Project Euler n°32:
We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once; for example, the 5-digit number, 15234, is 1 through 5 pandigital.
The product 7254 is unusual, as the identity, 39 × 186 = 7254, containing multiplicand, multiplier, and product is 1 through 9 pandigital.
Find the sum of all products whose multiplicand/multiplier/product identity can be written as a 1 through 9 pandigital.
My approach to check if the "equation" is pandigital is to concatenate the 3 numbers => sort them => then compare them to the list [1..9].
Concatenation:
the idea is to transform the numbers to strings and adding them easily
def List_to_str(source):
result = ''
for i in source:
result += str(i)
return result
Sorting:
We use the method sort(), and to do that we transform the string to a list of individual numbers
def str_to_list(source):
result = []
for i in source:
result.append(int(i))
result.sort()
return result
Comparing:
def Is_pandigital(number_list):
holder = [i for i in range(1,10)]
sorted_list = str_to_list(List_to_str(number_list))
if (len(sorted_list) != 9): return False
for i in range(len(sorted_list)):
if ( sorted_list[i] != holder[i]) : return False
return True
Finally the main function will loop through (i,j) and invoke the function Is_pandigital()
. To avoid summing the same number multiple times, as stated in the hint, I created a set()
to store the products of pandigital equation (sets are much more faster than lists).
Result :
def List_to_str(source):
result = ''
for i in source:
result += str(i)
return result
def str_to_list(source):
result = []
for i in source:
result.append(int(i))
result.sort()
return result
def Is_pandigital(number_list):
holder = [i for i in range(1,10)]
sorted_list = str_to_list(List_to_str(number_list))
if (len(sorted_list) != 9): return False
for i in range(len(sorted_list)):
if ( sorted_list[i] != holder[i]) : return False
return True
def main():
sum_pandigital = 0
product = set()
for i in range(2,100):
for j in range(2, 3000):
if ( Is_pandigital( [i,j,i*j] )) :
if ( not( i*j in product)) :
sum_pandigital += i*j
product.add(i*j)
print(product)
print(sum_pandigital)
main()
product = {5346, 5796, 6952, 7852, 4396, 7632, 7254} sum_pandigital = 45228
Hi! I am a robot. I just upvoted you! I found similar content that readers might be interested in:
http://www.mathblog.dk/project-euler-32-pandigital-products/
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Congratulations @wildweoweo! You received a personal award!
Click here to view your Board
Do not miss the last post from @steemitboard:
Vote for @Steemitboard as a witness and get one more award and increased upvotes!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Congratulations @wildweoweo! You received a personal award!
You can view your badges on your Steem Board and compare to others on the Steem Ranking
Do not miss the last post from @steemitboard:
Vote for @Steemitboard as a witness to get one more award and increased upvotes!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit