Search code examples
pythonpython-2.7palindrome

Palindrome (euler project)


I am trying solve this problem with Python:

"Find the largest palindrome made from the product of two 3-digit numbers." This is my code:

list_number = reversed(range(10000, 998002))


def check_number(x): 
    for element in reversed(range(100, 1000)):
        while True:
            if x >= 100:
                if element * x == i:
                    print(i)
                    break
                else:
                    x -= 1
                    continue
            else:
                break


for i in list_number:    
    if str(i) == str(i)[::-1]:
        check_number(999)
    else:
        continue

The code works with 2-digit numbers but not with 3-digit numbers. Can someone pleas tell me what is incorrect?


Solution

  • You code is extremely badly written, but the real problem is in the statement x -= 1: The value of x is decremented at each iteration of the inner loop and at the end of the first inner loop, x is 99. This makes the condition if x >= 100 false for all consequent iterations of the outer loop.

    Add x=999 immediately after for element in reversed(range(100, 1000)). Or, better, learn some good programming and rewrite the program entirely.