highest palindrome with 3 digit numbers in python

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

    ReThink: efficiency and performance

    def palindrome(n):    
    
        maxNumberWithNDigits = int('9' * n) #find the max number with n digits
    
        product = maxNumberWithNDigits * maxNumberWithNDigits 
    
        #Since we are looking the max, stop on the first match
    
        while True:        
            if str(product) == str(product)[::-1]: break;
    
            product-=1
    
        return product
    
    start=time.time()
    palindrome(3)
    end=time.time()-start
    

    palindrome...: 997799, 0.000138998031616 secs

提交回复
热议问题