Skip to content Skip to sidebar Skip to footer

Finding The Largest Palindrome Of The Product Of Two 3-digit Numbers In Python

So the challenge I'm trying to solve is to find the largest palindrome made from the product of two 3-digit numbers. I'm new to Python and so my code is not elegant or refracted ye

Solution 1:

Your code does a lot of unnecessary conversion between numbers and strings, which made the error hard to find. The only place in the code that needs a string representation is when determining if the number is a palindrome or not. So that should be the only place that the code does the conversion.

The logic error is in your function convert_to_num_list(). It takes a string representation of one number and returns a 1-list containing that number. So, "123321" gets returned as [123321]. You then take the max() of that 1-list, which is always the value that was passed to convert_to_num_list(). So the code never keeps the largest value because if a smaller value comes in later it will be overwritten. The code reports 995*583 as the largest because it comes in later than 993*913, which in turn is because 995 > 993.

You can fix that error with an if statement, but the program is overcomplicated and may well contain other bugs. I recommend reducing the code to the essential task of producing the largest palindrome, without printing out the intermediate results, because the simpler the code the easier it is to see a logic error.

defispalindrome(n):
    returnstr(n) == str(n)[::-1]

mylist=[]
for first_num inrange(100,1000):
    for second_num inrange(100,1000):
        item = first_num*second_num
        if ispalindrome(item):
            mylist.append(item)
print(max(mylist))

This gives your expected answer:

906609

Solution 2:

Here is a function for finding the largest palindrome of the product of two 3-digit numbers that I found in stackoverflow.

Link to what i found- https://stackoverflow.com/a/7460573

def is_pal(c):return int(str(c)[::-1])==c

   maxpal =0for a inrange(999,99,-1):for b inrange(a,99,-1):prod= a * b
           if is_pal(prod) and prod> maxpal:
               maxpal =prod

   print maxpal

Solution 3:

n1=999
n2=999
k=0
sl=[]
while n1>1:
  count=n1
  while count>=1:
    result=n1*count
    res=str(result)
    res1=res[::-1]
    if (res==res1):
      sl.insert(k,result)
      k+=1
    count=count-1
  n1=n1-1print("largest pelindrom of 3 digit product is is %d" %(max(sl)))

Solution 4:

palin=[]

for a in (range(1,1000)):
    for b in (range(1,1000)):
        d = a*b
        d=str(d)
        iflen(d)>5: 
            if d[0]==d[5]:
                    if d[1]==d[4]:
                        if d[2]==d[3]:
                            palin.append(d)
palin.sort()
print(palin[len(palin)-1])

Solution 5:

Using List comprehension can reduce the lines of code but i'll give an alternate option so that it's more readable.

List_of_palindromes = [i*j for i inrange(100,1000) for j inrange(i,1000) ifstr(i*j)==str(i*j)[::-1]]
print(max(List_of_palindromes))

More Readable form

List_of_palindromes = []
for i inrange(100,1000):
    for j inrange(100,1000):
        ifstr(i*j)==str(i*j)[::-1]:
            List_of_palindromes.append(i*j)
print(max(List_of_palindromes))

Post a Comment for "Finding The Largest Palindrome Of The Product Of Two 3-digit Numbers In Python"