highest palindrome with 3 digit numbers in python

后端 未结 13 1214
梦如初夏
梦如初夏 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

    Here is the function I made in python to check if the product of 3 digit number is a palindrome

    Function:

    def is_palindrome(x):
        i = 0
        result = True
        while i < int(len(str(x))/2):
            j = i+1
            if str(x)[i] == str(x)[-(j)]:
                result = True
            else:
                result = False
                break
            i = i + 1
        return result
    

    Main:


    max_pal = 0
    for i in range (100,999):
        for j in range (100,999):
            x = i * j
            if (is_palindrome(x)):
                if x > max_pal:
                    max_pal = x
    
    print(max_pal)
    

提交回复
热议问题