Skip to main content
replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

I would add a function is_palindrome, as @Dair@Dair already suggested in his answerhis answer. This saves you one call to str as well.

I would add a function is_palindrome, as @Dair already suggested in his answer. This saves you one call to str as well.

I would add a function is_palindrome, as @Dair already suggested in his answer. This saves you one call to str as well.

added 185 characters in body
Source Link
Graipher
  • 41.7k
  • 7
  • 70
  • 134

I would add a function is_palindrome, as @Dair already suggested in his answer. This saves you one call to str as well.

Final code:

import sys print(sys.version) ''' A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. This program intends to find the largest palindrome made from the product of two n-digit numbers. ''' def is_palindrome(number_str):: return number_str == number_str[::-1] digits = int(input("Enter no. of digits in multipliers : ")) min_plier = (10 ** (digits-1)) # Minimum n-digit number for eg. if digits = 3, min_plier = 100 max_plier = int("9" * digits) # Maximum n-digit number for eg. if digits = 3, max_plier = 999 # Calculate product and get palindrome max_palindrome = 0 for z in range (max_plier, min_plier , -1): for x in range(z, min_plier, -1):   global pallindromes  product = z * x # Check if product obtained is palindrome and is greater than previously obtained palindrome. if is_palindrom(str(product) == str(product)[::-1]): max_palindrome = max(max_palindrome, product)  print("Largest palindrome is: {}".format(max_palindrome)) 

Final code:

import sys print(sys.version) ''' A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. This program intends to find the largest palindrome made from the product of two n-digit numbers. ''' digits = int(input("Enter no. of digits in multipliers : ")) min_plier = (10 ** (digits-1)) # Minimum n-digit number for eg. if digits = 3, min_plier = 100 max_plier = int("9" * digits) # Maximum n-digit number for eg. if digits = 3, max_plier = 999 # Calculate product and get palindrome max_palindrome = 0 for z in range (max_plier, min_plier , -1): for x in range(z, min_plier, -1):   global pallindromes  product = z * x # Check if product obtained is palindrome and is greater than previously obtained palindrome. if (str(product) == str(product)[::-1]): max_palindrome = max(max_palindrome, product)  print("Largest palindrome is: {}".format(max_palindrome)) 

I would add a function is_palindrome, as @Dair already suggested in his answer. This saves you one call to str as well.

Final code:

import sys print(sys.version) ''' A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. This program intends to find the largest palindrome made from the product of two n-digit numbers. ''' def is_palindrome(number_str):: return number_str == number_str[::-1] digits = int(input("Enter no. of digits in multipliers : ")) min_plier = (10 ** (digits-1)) # Minimum n-digit number for eg. if digits = 3, min_plier = 100 max_plier = int("9" * digits) # Maximum n-digit number for eg. if digits = 3, max_plier = 999 # Calculate product and get palindrome max_palindrome = 0 for z in range (max_plier, min_plier , -1): for x in range(z, min_plier, -1): product = z * x # Check if product obtained is palindrome and is greater than previously obtained palindrome. if is_palindrom(str(product)): max_palindrome = max(max_palindrome, product) print("Largest palindrome is: {}".format(max_palindrome)) 
Source Link
Graipher
  • 41.7k
  • 7
  • 70
  • 134

This seems a bit too complicated to get 999 when entering 3, 9999, when entering 4, ...

max_plier = int(("9" * (digits+1))[:digits]) 

It is sufficient to do:

max_plier = int("9" * digits) 

Also note that in Python ; at the end of the line are completely superfluous and their use is therefore frowned upon. Even though they are perfectly legal Python.

You keep track of the whole list of palindromes, only to calculate the max at the end. This will consume a lot of memory, especially when the numbers get bigger. Just keep track of a running maximum instead. There is also no need for the global keyword, since your whole code is in the same scope.

You can let the inner loop just run from z, instead of setting max_plier = z.

I would use str.format to display the final result.

Final code:

import sys print(sys.version) ''' A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. This program intends to find the largest palindrome made from the product of two n-digit numbers. ''' digits = int(input("Enter no. of digits in multipliers : ")) min_plier = (10 ** (digits-1)) # Minimum n-digit number for eg. if digits = 3, min_plier = 100 max_plier = int("9" * digits) # Maximum n-digit number for eg. if digits = 3, max_plier = 999 # Calculate product and get palindrome max_palindrome = 0 for z in range (max_plier, min_plier , -1): for x in range(z, min_plier, -1): global pallindromes product = z * x # Check if product obtained is palindrome and is greater than previously obtained palindrome. if (str(product) == str(product)[::-1]): max_palindrome = max(max_palindrome, product) print("Largest palindrome is: {}".format(max_palindrome)) 

There also seems to be a bug in your looping. You never check x * min_plier, because of the way the for loop works. You could iterate up to min_plier - 1. Or you note that that number will never be a palindrome anyways (it starts with a 1 and ends with a 0), and realize that it is not a real bug.