highest palindrome with 3 digit numbers in python

后端 未结 13 1227
梦如初夏
梦如初夏 2021-02-01 10:35

In problem 4 from http://projecteuler.net/ it says:

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-d

13条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-01 11:30

    This would more efficiently be written as:

    from itertools import product
    
    def is_palindrome(num):
        return str(num) == str(num)[::-1]
    
    multiples = ( (a, b) for a, b in product(xrange(100,999), repeat=2) if is_palindrome(a*b) )
    print max(multiples, key=lambda (a,b): a*b)
    # (913, 993)
    

    You'll find itertools and generators very useful if you're doing Euler in Python.

提交回复
热议问题