Home > Project Euler, python > Problem Euler #32

Problem Euler #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.

python:

import time

ts = time.time()

def is_pan(n):
    digits = range(1,10)
    num = [int(n) for n in str(n)]
    num.sort()
    return num == digits

prods = set()
      
for a in xrange(1, 100):
    if a > 9: low = 123
    else: low = 1234
    for b in xrange(low, 10001/a):
        res = '{}{}{}'.format(a, b, a*b)
        if is_pan(res):
            prods.add(a*b)

print sum(prods)
print time.time() - ts

Categorie:Project Euler, python
  1. Non c'è ancora nessun commento.
  1. No trackbacks yet.

Lascia un commento

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...

%d blogger cliccano Mi Piace per questo: