반응형
39 x 186 = 7254 란 등식에는 1부터 9까지의 모든 수가 등장한다. A x B = C 꼴이 되고 1부터 9까지의 숫자를 한번씩 모두 사용할 수 있도록 표현되는 C가 되는 모든 C의 합은 얼마일까? (A' x B' = A x B = C 인 C도 있으나 한번만 합산한다.)
무한 for문 겹치기 신공으로 무식하게 풀었다.
#!/usr/bin/env python
# http://projecteuler.net/index.php?section=problems&id=32
#
# Solve
#
#### abc ####
#### x de ####
#### ----- ####
#### fghi ####
#
# Or
#
#### abcd ####
#### x e ####
#### ----- ####
#### fghi ####
#
# Maybe stupid, but simple and clean!
list = [ i for i in range(1, 10) ]
products = []
for a in list:
list0 = list[0:]
list0.remove(a)
for b in list0:
list1 = list0[0:]
list1.remove(b)
for c in list1:
list2 = list1[0:]
list2.remove(c)
for d in list2:
list3 = list2[0:]
list3.remove(d)
for e in list3:
list4 = list3[0:]
list4.remove(e)
for f in list4:
list5 = list4[0:]
list5.remove(f)
for g in list5:
list6 = list5[0:]
list6.remove(g)
for h in list6:
list7 = list6[0:]
list7.remove(h)
for i in list7:
if 1000*f + 100*g + 10*h + i == 1000*a*d + 100*b*d + 10*c*d + 100*a*e + 10*b*e + c*e:
print "%d%d%d x %d%d = %d%d%d%d"%(a,b,c,d,e,f,g,h,i)
if not 1000*f + 100*g + 10*h + i in products:
products.append(1000*f + 100*g + 10*h + i)
if 1000*f + 100*g + 10*h + i == 1000*a*e + 100*b*e + 10*c*e + d*e:
print "%d%d%d%d x %d = %d%d%d%d"%(a,b,c,d,e,f,g,h,i)
if not 1000*f + 100*g + 10*h + i in products:
products.append(1000*f + 100*g + 10*h + i)
total = 0
for sum in products:
total += sum
print products
print total
728x90
'프로그래밍 > 알고리즘' 카테고리의 다른 글
[EP 074] f(145) = 1! + 4! + 5! = 145 (0) | 2021.02.09 |
---|---|
1/1, 1/2, ... 1/999 의 순환소수 표현 (4) | 2020.12.02 |
[EP 102] 삼각형 내부외부 판별 (0) | 2020.11.24 |
[EP 018] 삼각형 배열에서 지나간 수의 합을 최대로 하는 경로를 구하기 (0) | 2020.11.24 |
[EP0010] 백만이하 소수의 합 (0) | 2020.11.24 |