P4

# -*- coding=utf-8 -*-
#!/usr/bin/python
'''
A palindromic number reads the same both ways.
The largest palindrome made from the product of two 2-digit numbers 
is 9009 = 91 × 99.
Find the largest palindrome made from the product of two 3-digit numbers.

'''
import time

def ispal (n):
    if n <= 99999:
        x = n // 1000
        y = ( n % 10) * 100 + ( (n // 10) % 10 )
        if x == y:
            return True
    else:
       x = n // 1000
       y = (n% 10) * 100 + ((n//10)%10)* 10 + ((n // 100)%10 )
       if x == y:
           return True
    return False

def palindromic ():
    result = 0
    for i in range(999, 100, -1) :
        for j in range(999, 100, -1):
            if ispal( i*j ):
                if result < i*j:
                    result = i * j
                    result_i = i
                    result_j = j
    print "%d * %d = %d" %(result_i, result_j, result)

if __name__ == '__main__':
    tStart = time.time()
    palindromic()
    tEnd = time.time()
    print 'cost time:', tEnd - tStart

运行时间:

P4_第1张图片


等过几天在看看别人是怎么做的,学习一下。这是最笨的方法。

你可能感兴趣的:(P4)