2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Project Euler 4

Last updated at Posted at 2015-02-25

左右どちらから読んでも同じ値になる数を回文数という. 2桁の数の積で表される回文数のうち, 最大のものは 9009 = 91 × 99 である.

では, 3桁の数の積で表される回文数の最大値を求めよ.
http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%204

本能気ままに解いた。正解かどうかは確認していない。

(i,j)=(999,999)
max=1

while i>0:
    while j>0:
        k = i*j
        if k <= max: break
        if str(k)==str(k)[::-1]: max=k
        j -= 1
    (i,j)=(i-1,999)
print max

ほかにありうるとすれば、1. 3桁+逆順3桁で6桁の回文数を作成し、それが3桁×3桁で表されるかどうかをチェックする 2. リスト内包表記を使ってみる、の2点だろうか。後で試してみよう。

2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?