highest palindrome with 3 digit numbers in python

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

    Each time it doesnot have to start from 999 as it is already found earlier.Below is a simple method using string function to find largest palindrome using three digit number

    def palindrome(y):
        z=str(y)
        w=z[::-1]
        if (w==z):
            return 0
        elif (w!=z):
            return 1        
    h=[]
    a=999
    for i in range (999,0,-1):
        for j in range (a,0,-1):
        l=palindrome(i*j)
        if (l==0):
            h=h+[i*j]               
        a-=1
    print h
    max=h[0]
    
    for i in range(0,len(h)):
        if (h[i] > max):
        max= h[i]
    print "largest palindrome using multiple of three digit number=%d"%max  
    

提交回复
热议问题