プロジェクトオイラー4
プロジェクトオイラーの問題4をpythonで解きました。
問題
3桁の自然数×3桁の自然数で回文数(palindrome)になる数字の中で、最も大きい数字を求めよ。
def palindrome(n): #palindrome関数 from collections import deque dq = deque(n) while len(dq) > 1: if dq.popleft() != dq.pop(): return False return True rows = range(100, 999) cols = range(100, 999) matrix = [(row, col) for row in rows for col in cols if row >= col] palindromes_list =[] for element in matrix: #3桁×3桁のpalindromeのリスト number = element[0] * element[1] if palindrome(str(number)): palindromes_list.append(number) answer = max(palindromes_list) #3桁×3桁でのpalindromeで最大のもの print(answer)