Wildre avatar

Project Euler N 32 | the dumb way

wildweoweo

Published: 11 Mar 2018 › Updated: 11 Mar 2018Project Euler N 32 | the dumb way

Project Euler N 32 | the dumb way

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.

HINT: Some products can be obtained in more than one way so be sure to only include it once in your sum.

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

Leave Project Euler N 32 | the dumb way to:

Written by

Read more #python posts


Best Posts From Wildre

We have not curated any of wildweoweo's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From Wildre