The number 1406357289 is a Pandigital number from 0 to 9 (because 0 to 9 appear once). This number has an interesting subsequence property.
Let d1 be the first digit and d2 be the second digit, and define dn in the following order. Using this notation, we can see the following.
d2d3d4 =406 is divisible by 2
d3d4d5 =063 is divisible by 3
d4d5d6 =635 is divisible by 5
d5d6d7 =357 is divisible by 7
d6d7d8 =572 is divisible by 11
d7d8d9 =728 is divisible by 13
d8d9d10 =289 is divisible by 17
Find the sum of the Pandigital numbers from 0 to 9 with this property. http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2043
Coding was done using the previously created Pandigital number generator. But it's quite late.
# coding: utf-8
import copy
import math
def pandigital(digit,seq1,seq2=[]):
iter1 = map(str,seq1)
if seq2:
iter2 = map(str,seq2)
else:
iter2 = copy.deepcopy(iter1)
for d in range(digit-1):
iter1 = (x+y for x in iter1 for y in iter2 if not (y in x))
return iter1
def is_divisible(n):
div_list = [2, 3, 5, 7, 11, 13, 17]
for d in range(2,9):
if (int(n[d-1:d+2]) % div_list[d-2]) != 0:
return False
return True
def main():
digit = 10
ans = 0
for pdg in pandigital(digit,range(digit),range(digit)):
if is_divisible(pdg):
ans += int(pdg)
print ans
Recommended Posts