Sudal's Garage

Project Euler 43 본문

Programming/Project Euler - python

Project Euler 43

_Sudal 2019. 4. 5. 05:00

Question: 


The number, 1406357289, is a 0 to 9 pandigital number because it is made up of each of the digits 0 to 9 in some order, but it also has a rather interesting sub-string divisibility property.

Let d1 be the 1st digit, d2 be the 2nd digit, and so on. In this way, we note 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 all 0 to 9 pandigital numbers with this property.


문제:


다음 숫자, 1406357289, 는 0부터 9 까지의 팬디지털 숫자이다.

0부터 9까지의 수를 모두 사용하였을 뿐 만 아니라, 다음과 같은 흥미로운 성질을 가지고 있다.

d를 1번째 자릿수d2 를 2번째 자릿수, ... dn 을 n번째 자릿수라고 하자.

  • d2d3d4=406 는 2로 나눠진다.
  • d3d4d5=063 는 3으로 나눠진다.
  • d4d5d6=635 는 5로 나눠진다.
  • d5d6d7=357 는 7로 나눠진다.
  • d6d7d8=572 는 11로 나눠진다.
  • d7d8d9=728 는 13으로 나눠진다.
  • d8d9d10=289 는 17로 나눠진다.

이러한 성질을 가지고 있는 0부터 9까지의 팬디지털 수들의 합을 구하라.


Solution:


from itertools import permutations
import time

def getlen(n):
    return len(list(str(n)))

def check_pandi(n):
    for i in range(0,getlen(n)):
        if str(i) not in list(str(n)):
            return False
    return True

nums = '0123456789'
primes = [2,3,5,7,11,13,17]
result = list()
start = time.time()

for i in permutations(nums):
    x = list(i)
    
    if not x[0] == '0':
        condition = True
        
        for j in range(1,8):
            if (100 * int(x[j]) + 10 * int(x[j+1]) + int(x[j+2])) % primes[j-1] == 0:
                pass
            else:
                condition = False
                break
        
        if condition:
            result.append(int(''.join(x)))

print('Sum of numbers: {0}\nFound in ...{1:.2f}s'.format(sum(result),time.time()-start))



9초정도 걸렸다..




'Programming > Project Euler - python' 카테고리의 다른 글

Project Euler 45  (0) 2019.04.07
Project Euler 44  (0) 2019.04.06
Project Euler 42  (0) 2019.04.04
Project Euler 41  (0) 2019.04.03
Project Euler 40  (0) 2019.04.02