Programming/Project Euler - python
Project Euler 4
_Sudal
2019. 1. 30. 08:57
Question:
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
Find the largest palindrome made from the product of two 3-digit numbers.
문제:
앞에서부터 읽을 때나 뒤에서부터 읽을 때나 모양이 같은 수를 대칭수(palindrome)라고 부릅니다.
두 자리 수를 곱해 만들 수 있는 대칭수 중 가장 큰 수는 9009 (= 91 × 99) 입니다.
세 자리 수를 곱해 만들 수 있는 가장 큰 대칭수는 얼마입니까?
Solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import time def isPalin(num): i = 0 while i < len(list(str(num))) / 2: if int(list(str(num))[i]) == int(list(str(num))[-(i+1)]): i += 1 else: return False return True result = 0 start = time.time() for i in reversed(list(range(900,1000))): for j in reversed(list(range(900,1000))): if isPalin(i*j): result = i*j break if result != 0: break print([i,j,result,time.time() - start]) |
isPalin function 은 num 이 palindrome number 인지 확인해주는 함수.
코딩을 시작한지 얼마 안되어서 그런지 멍청한 방법으로 확인했다...
1 2 3 4 | def isPalin(num): if list(str(num)) == list(reversed(str(num))): return True return False | cs |
그냥 이렇게 뒤집어 버리면 되는 데..
나머지 코드는 너무 간단하니 패스!