highest palindrome with 3 digit numbers in python

故事扮演 提交于 2019-12-02 18:37:21

Iterating in reverse doesn't find the largest x*y, it finds the palindrome with the largest x. There's a larger answer than 580085; it has a smaller x but a larger y.

This would more efficiently be written as:

from itertools import product

def is_palindrome(num):
    return str(num) == str(num)[::-1]

multiples = ( (a, b) for a, b in product(xrange(100,999), repeat=2) if is_palindrome(a*b) )
print max(multiples, key=lambda (a,b): a*b)
# (913, 993)

You'll find itertools and generators very useful if you're doing Euler in Python.

Not the most efficient answer but I do like that it's compact enough to fit on one line.

print max(i*j for i in xrange(1,1000) for j in xrange(1,1000) if str(i*j) == str(i*j)[::-1])

Tried making it more efficient, while keeping it legible:

def is_palindrome(num):
    return str(num) == str(num)[::-1]

def fn(n):
    max_palindrome = 1
    for x in range(n,1,-1):
        for y in range(n,x-1,-1):
            if is_palindrome(x*y) and x*y > max_palindrome:
                max_palindrome = x*y
            elif x * y < max_palindrome:
                break
    return max_palindrome

print fn(999)

Here I added two 'break' to improve the speed of this program.

def is_palindrome(num):
    return str(num) == str(num)[::-1]
def max_palindrome(n):
    max_palindrome = 1
    for i in range(10**n-1,10**(n-1)-1,-1):
        for j in range(10**n-1,i-1,-1):
            if is_palindrome(i*j) and i*j > max_palindrome:
                max_palindrome = i * j
                break
            elif i*j < max_palindrome:
                break
    return max_palindrome
n=int(raw_input())
print max_palindrome(n)

Simple:

def is_pallindrome(n):
    s = str(n)
    for n in xrange(1, len(s)/2 + 1):
        if s[n-1] != s[-n]:
            return False    
    return True

largest = 0
for j in xrange(100, 1000):
    for k in xrange(j, 1000):
        if is_pallindrome(j*k):
            if (j*k) > largest: largest = j*k
print largest

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  

Here is my code to solve this problem.

lst = []
for i in range(100,1000): 
    for n in range(2,i) : 
        lst.append (i* n) 
        lst.append(i*i)

lst2=[]
for i in lst: 
    if str(i) == str(i)[::-1]:
        lst2.append(i)
print max(lst2) 

580085 = 995 X 583, where 906609 = 993 X 913 found it only by applying brute forcing from top to bottom!

Here is my Python code:

max_pal = 0
for i in range(100,999):
    for j in range(100,999):
      mult = i * j 
      if str(mult) == str(mult)[::-1]: #Check if the number is palindrome
          if mult > max_pal: 
              max_pal = mult
print (max_pal)
def div(n):
    for i in range(999,99,-1):
        if n%i == 0:
            x = n/i
            if x % 1 == 0:
                x = n//i
                if len(str(x)) == 3:
                    print(i)
                    return True
    return False


def palindrome():
    ans = []
    for x in range(100*100,999*999+1):
        s = str(x)
        s = int (s[::-1])
        if x - s == 0:
            ans.append(x)

    for x in range(len(ans)):
        y = ans.pop()
        if div(y):
            return y


print(palindrome())

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!