LoginSignup
2
2

More than 5 years have passed since last update.

会社で少し盛り上がった Project Euler をやってみる 004

Last updated at Posted at 2018-01-25

Problem 004

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


Answer 004 (Python)

class Problem4:

    def main(self):
        palindromeNumber = 0
        for i in range(100, 1000):
            for j in range(i, 1000):
                num = i * j
                if self.isPalindromeNumber(num):
                    if num > palindromeNumber:
                        palindromeNumber = num
        print(palindromeNumber)

    def isPalindromeNumber(self, num):
        l = list(str(num))
        for i in range(0, int(len(l) / 2)):
            if l[i] != l[-1-i]:
                return False
        return True


if __name__ == '__main__':
    p = Problem4()
    p.main()

(参考) Project Euler とは

Project Euler はプログラミングで数学の問題を解くサイトです。問題は600問以上あるので、勉強に使うのも良し、楽しむのも良しです。

サイトに登録すれば、解答を submit することができ、その場であっているか判定してくれます。

また、問題だけであれば、日本語に翻訳された Wiki もあるのでそちらを見ながらやると捗ると思います。

Project Euler(日本語訳)

2
2
2

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