Skip to main content
edited tags; edited tags
Link
200_success
  • 145.7k
  • 22
  • 191
  • 481
Rollback to Revision 1
Source Link
AJNeufeld
  • 35.3k
  • 5
  • 41
  • 103
def largest_palindrome_product(n:int) -> int:
    '''
    Returns largest palindrome whose a product of two n digit(base 10) integer
    :param n: the number of digits in the numbers we compute the product of
    :return: largest palindrome whose a product of two n digit(base 10) integer or -1 if non were found
    '''
    # Dealing with edge cases
    if n == 1:
        return 9
    elif n < 1:
        raise ValueError("Expecting n to be >= 1")

    max_dictmul_max = {'max': -1, 'i': -1,'j': -1}
    upper_boundary = (10**n) - 1
    lower_boundary = 10**(n-1)

    # Searching for the largest palindrome between the upper boundary and the lower one.
    for i in range(upper_boundary, lower_boundary-1, -1):
        for j in range(i, lower_boundary-1, -1):
            # Even if this is a palindrom we will not return it therefor we dont need to do the computation in the loop
            if i <= max_dict['i'] and j <= max_dict['j']:
                continue
            prod = i*j
            str_prod = str(prodi*j)
            if prodi*j > max_dict['max']mul_max and str_prod[::-1] == str_prod:
                max_dict['max']mul_max = prodi*j
                max_dict['i'] = i
                max_dict['j'] = j
    return max_dict['max']mul_max

Here'sHere is a simple Testsmall test case for the solution abovethis code:

from ProjectEuler.problem4 import largest_palindrome_product

if __name__ == "__main__":
    # largest prime product is of 91*99 -> returns 9009
    print(largest_palindrome_product(2))
    # largest prime product isChecking ofedge 9*1cases -> returns 9
    print(largest_palindrome_product(1))
    # largest prime product is of 993*913 -> returns 906609
    print(largest_palindrome_product(3))

Edit: Ive changed the code according to all the suggestions made by AJNeufeld, Thanks! Hope its better now. Let me know your thoughts on this solution :)

def largest_palindrome_product(n:int) -> int:
    '''
    Returns largest palindrome whose a product of two n digit(base 10) integer
    :param n: the number of digits in the numbers we compute the product of
    :return: largest palindrome whose a product of two n digit(base 10) integer or -1 if non were found
    '''
    # Dealing with edge cases
    if n < 1:
        raise ValueError("Expecting n to be >= 1")

    max_dict = {'max': -1, 'i': -1,'j': -1}
    upper_boundary = (10**n) - 1
    lower_boundary = 10**(n-1)

    # Searching for the largest palindrome between the upper boundary and the lower one.
    for i in range(upper_boundary, lower_boundary-1, -1):
        for j in range(i, lower_boundary-1, -1):
            # Even if this is a palindrom we will not return it therefor we dont need to do the computation in the loop
            if i <= max_dict['i'] and j <= max_dict['j']:
                continue
            prod = i*j
            str_prod = str(prod)
            if prod > max_dict['max'] and str_prod[::-1] == str_prod:
                max_dict['max'] = prod
                max_dict['i'] = i
                max_dict['j'] = j
    return max_dict['max']

Here's a simple Test case for the solution above:

from ProjectEuler.problem4 import largest_palindrome_product

if __name__ == "__main__":
    # largest prime product is of 91*99 -> returns 9009
    print(largest_palindrome_product(2))
    # largest prime product is of 9*1 -> returns 9
    print(largest_palindrome_product(1))
    # largest prime product is of 993*913 -> returns 906609
    print(largest_palindrome_product(3))

Edit: Ive changed the code according to all the suggestions made by AJNeufeld, Thanks! Hope its better now.

def largest_palindrome_product(n:int) -> int:
    '''
    Returns largest palindrome whose a product of two n digit(base 10) integer
    :param n: the number of digits in the numbers we compute the product of
    :return: largest palindrome whose a product of two n digit(base 10) integer or -1 if non were found
    '''
    # Dealing with edge cases
    if n == 1:
        return 9
    elif n < 1:
        raise ValueError("Expecting n to be >= 1")

    mul_max = -1
    upper_boundary = (10**n) - 1
    lower_boundary = 10**(n-1)

    # Searching for the largest palindrome between the upper boundary and the lower one.
    for i in range(upper_boundary, lower_boundary, -1):
        for j in range(i, lower_boundary, -1):
            str_prod = str(i*j)
            if i*j > mul_max and str_prod[::-1] == str_prod:
                mul_max = i*j

    return mul_max

Here is a small test case for this code:

from ProjectEuler.problem4 import largest_palindrome_product

if __name__ == "__main__":
    # largest prime product is of 91*99 -> returns 9009
    print(largest_palindrome_product(2))
    # Checking edge cases -> returns 9
    print(largest_palindrome_product(1))
    # largest prime product is of 993*913 -> returns 906609
    print(largest_palindrome_product(3))

Let me know your thoughts on this solution :)

added 432 characters in body
Source Link
def largest_palindrome_product(n:int) -> int:
    '''
    Returns largest palindrome whose a product of two n digit(base 10) integer
    :param n: the number of digits in the numbers we compute the product of
    :return: largest palindrome whose a product of two n digit(base 10) integer or -1 if non were found
    '''
    # Dealing with edge cases
    if n == 1:
        return 9
    elif n < 1:
        raise ValueError("Expecting n to be >= 1")

    mul_maxmax_dict = {'max': -1, 'i': -1,'j': -1}
    upper_boundary = (10**n) - 1
    lower_boundary = 10**(n-1)

    # Searching for the largest palindrome between the upper boundary and the lower one.
    for i in range(upper_boundary, lower_boundary-1, -1):
        for j in range(i, lower_boundary-1, -1):
            # Even if this is a palindrom we will not return it therefor we dont need to do the computation in the loop
            if i <= max_dict['i'] and j <= max_dict['j']:
                continue
            prod = i*j
            str_prod = str(i*jprod)
            if i*jprod > mul_maxmax_dict['max'] and str_prod[::-1] == str_prod:
                mul_maxmax_dict['max'] = i*jprod
                max_dict['i'] = i
                max_dict['j'] = j
    return mul_maxmax_dict['max']

Here isHere's a small testsimple Test case for this codethe solution above:

from ProjectEuler.problem4 import largest_palindrome_product

if __name__ == "__main__":
    # largest prime product is of 91*99 -> returns 9009
    print(largest_palindrome_product(2))
    # Checkinglargest edgeprime casesproduct is of 9*1 -> returns 9
    print(largest_palindrome_product(1))
    # largest prime product is of 993*913 -> returns 906609
    print(largest_palindrome_product(3))

Let me know your thoughts on this solution :)Edit: Ive changed the code according to all the suggestions made by AJNeufeld, Thanks! Hope its better now.

def largest_palindrome_product(n:int) -> int:
    '''
    Returns largest palindrome whose a product of two n digit(base 10) integer
    :param n: the number of digits in the numbers we compute the product of
    :return: largest palindrome whose a product of two n digit(base 10) integer or -1 if non were found
    '''
    # Dealing with edge cases
    if n == 1:
        return 9
    elif n < 1:
        raise ValueError("Expecting n to be >= 1")

    mul_max = -1
    upper_boundary = (10**n) - 1
    lower_boundary = 10**(n-1)

    # Searching for the largest palindrome between the upper boundary and the lower one.
    for i in range(upper_boundary, lower_boundary, -1):
        for j in range(i, lower_boundary, -1):
            str_prod = str(i*j)
            if i*j > mul_max and str_prod[::-1] == str_prod:
                mul_max = i*j

    return mul_max

Here is a small test case for this code:

from ProjectEuler.problem4 import largest_palindrome_product

if __name__ == "__main__":
    # largest prime product is of 91*99 -> returns 9009
    print(largest_palindrome_product(2))
    # Checking edge cases -> returns 9
    print(largest_palindrome_product(1))
    # largest prime product is of 993*913 -> returns 906609
    print(largest_palindrome_product(3))

Let me know your thoughts on this solution :)

def largest_palindrome_product(n:int) -> int:
    '''
    Returns largest palindrome whose a product of two n digit(base 10) integer
    :param n: the number of digits in the numbers we compute the product of
    :return: largest palindrome whose a product of two n digit(base 10) integer or -1 if non were found
    '''
    # Dealing with edge cases
    if n < 1:
        raise ValueError("Expecting n to be >= 1")

    max_dict = {'max': -1, 'i': -1,'j': -1}
    upper_boundary = (10**n) - 1
    lower_boundary = 10**(n-1)

    # Searching for the largest palindrome between the upper boundary and the lower one.
    for i in range(upper_boundary, lower_boundary-1, -1):
        for j in range(i, lower_boundary-1, -1):
            # Even if this is a palindrom we will not return it therefor we dont need to do the computation in the loop
            if i <= max_dict['i'] and j <= max_dict['j']:
                continue
            prod = i*j
            str_prod = str(prod)
            if prod > max_dict['max'] and str_prod[::-1] == str_prod:
                max_dict['max'] = prod
                max_dict['i'] = i
                max_dict['j'] = j
    return max_dict['max']

Here's a simple Test case for the solution above:

from ProjectEuler.problem4 import largest_palindrome_product

if __name__ == "__main__":
    # largest prime product is of 91*99 -> returns 9009
    print(largest_palindrome_product(2))
    # largest prime product is of 9*1 -> returns 9
    print(largest_palindrome_product(1))
    # largest prime product is of 993*913 -> returns 906609
    print(largest_palindrome_product(3))

Edit: Ive changed the code according to all the suggestions made by AJNeufeld, Thanks! Hope its better now.

Source Link
Loading