0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Project Euler】Problem 4: 回文数になる積

Last updated at Posted at 2022-01-02
  • 本記事はProjectEulerの「100番以下の問題の説明は記載可能」という規定に基づいて解答のヒントが書かれていますので、自分である程度考えてみてから読まれることをお勧めします

問題 4: 回文数になる積

原文 Problem 4: Largest palindrome product

問題の要約:2個の3桁の数を掛けて回文数(palindrome)になる数の最大値を求めよ

まず回文数チェック関数isPalindを作ります。数字だけでなく文字列全般に使えるように入力は文字列です。文字列も逆順は$[::-1]$で得られます

def isPalind(s):   # s: string
  return s==s[::-1]

後は3桁の数の積のすべての組み合わせをitertoolsのcombinations_with_replacementを使って生成し回文数チェックを行います

import itertools
mpr = 0
for i, j in itertools.combinations_with_replacement(range(100,1000), 2):
  pr = i*j
  if isPalind(str(pr)) and pr > mpr: 
    mpr = pr
print(mpr)     

(開発環境:Google Colab)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?