社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

两个n位数的乘积的最大回文(python)

Eye Sun • 7 年前 • 1923 次点击  

这是我的实现,但是当给定6位数字时效率不高。

Input  : n = 2
Output : 9009 

9009是最大的数字,它是两个

两位数。9009=91*99。

def isPali(x):
n = str(x)
for i in range(len(n)):
    if not n[i] == n[-i-1]:
        return False
return True

def isProduct(x,A):
counter = A
while counter > 1:
    if x // counter <= A and x % counter == 0:
        return True
    else:
        counter-=1
return False

def largestProduct(A):
for i in range(A*A,1,-1):
    if isPali(i) and isProduct(i,A):
        return i
return False

largestProduct(999999)
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/46949
文章 [ 2 ]  |  最新文章 7 年前