문제 : 앞에서부터 읽을 때나 뒤에서부터 읽을 때나 모양이 같은 수를 대칭수(palindrome)라고 부릅니다.
두 자리 수를 곱해 만들 수 있는 대칭수 중 가장 큰 수는 9009 (= 91 × 99) 입니다.
세 자리 수를 곱해 만들 수 있는 가장 큰 대칭수는 얼마입니까?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | def palindrome(): i = 100 comp = 0 while i<=1000: j=100 while j<=1000: chk = False cal = str(i*j) #print cal length = len(cal) for cnt in range(0, length/2): if cal[cnt] == cal[length-cnt-1]: chk = True continue else: chk = False break if chk == True: if comp < i*j: comp = i*j print "%d x %d = %s" % (i, j, cal) j+=1 i+=1 print "maximu palindrome val: %d" % comp palindrome() | cs |
974 x 308 = 299992
975 x 539 = 525525
977 x 638 = 623326
978 x 209 = 204402
978 x 418 = 408804
979 x 337 = 329923
979 x 387 = 378873
979 x 446 = 436634
979 x 496 = 485584
979 x 555 = 543345
979 x 605 = 592295
979 x 614 = 601106
979 x 664 = 650056
982 x 869 = 853358
987 x 143 = 141141
987 x 286 = 282282
991 x 121 = 119911
993 x 913 = 906609
995 x 517 = 514415
995 x 583 = 580085
maximum val : 906609
'* Computer Science > Project Euler' 카테고리의 다른 글
project euler 6. problem (0) | 2016.07.16 |
---|---|
project euler 5. problem (0) | 2016.07.15 |
project euler 3. problem (0) | 2016.07.13 |
project euler 2. problem (0) | 2016.07.12 |
project euler 1. problem (0) | 2016.07.11 |