[PYTHON] Project Euler 43

problem

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

Answer

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

Project Euler 7
Project Euler 47
Project Euler 31
Project Euler 38
Project Euler 17
Project Euler 8
Project Euler 23
Project Euler 22
Project Euler 19
Project Euler 50
Project Euler 42
Project Euler 32
Project Euler 43
Project Euler 35
Project Euler 36
Project Euler 24
Project Euler 46
Project Euler 48
Project Euler 45
Project Euler 6
Project Euler 44
Project Euler 39
Project Euler 40
Project Euler 49
Project Euler 29
Project Euler 27
Project Euler 41
Project Euler 18
Project Euler 13
Project Euler 30
Project Euler 16
Project Euler 14
Project Euler 34
Project Euler 25
[Project Euler] problem1
Project Euler15 "Lattice Path"
Project Euler Original Method Group 1
What is Project Euler 3 Acceleration?
[Note] Project Euler in Python (Problem 1-22)
Functional programming in Python Project Euler 3
Project Euler # 5 "Minimum Multiples" in Python
Project Euler 4 Attempt to speed up
Functional programming in Python Project Euler 2
Project Euler 11 "Maximum product in grid"
Project Euler # 15 "Lattice Path" in Python
Project Euler # 4 "Maximum Palindrome" in Python
Project Euler 9 Retention of calculation results
Project Euler # 3 "Maximum Prime Factors" in Python
Project Euler # 11 "Maximum Product in Grid" in Python
Project Euler # 7 "1000 1st prime number" in Python
Project Euler # 16 "Sum of Powers" in Python
Project Euler # 9 "Special Pythagorean Triple" in Python
Project Euler # 14 "Longest Collatz Sequence" in Python
I wrote Project Euler 1 in one liner.
Project Euler # 2 "Even Fibonacci Numbers" in Python
Project Euler # 17 "Number of Characters" in Python
Project Euler # 1 "Multiples of 3 and 5" in Python
Project Euler # 8 "Maximum Product in Number String" in Python
Project Euler # 10 "sum of prime numbers" in Python
Project Euler 28 Inefficient Answer Proposal Create "Spiral Numbers"
Project Euler 4 Coding with a new approach fails.