0

I'm trying to find largest palindromic number made from the product of two 3-digit numbers with my code. it works fine for 2-digit and 3-digit, but when I try it with 4-digit numbers, it doesn't work anymore. There is no output or "Process finished with exit code 0" at the end. It just halted like it was in an infinite loop.

palin = 0
for x in range(1, 10000):
    for y in range(1, 10000):
        mult = x * y

        if str(mult) == str(mult)[::-1]:
            if mult > palin:
                palin = mult

print(palin)

where did I go wrong? I just started Python about a month ago, so my code is still not effective

G. Hally
  • 25
  • 1
  • 4
  • It's kinda an infinite loop, you know, it's very long... – U13-Forward Nov 02 '18 at 02:34
  • 1
    Your algorithm does not halt. It's slow (and it's wrong). So you have to *wait for some minutes* before getting any output. – Hoblovski Nov 02 '18 at 02:35
  • 1
    It's the 4th problem in PrjectEuleur's website called `Largest palindrome product`. Your algorithm isn't correct and it's not an infinite loop. But it may take a long time to finish the calculations. Better way is optimizing your code. Maybe with some maths... – Chiheb Nexus Nov 02 '18 at 02:56

2 Answers2

2

Your algorithm is not correct. The second if should be at the same level as mult = x*y.

I modified your code a little bit. With the code below you see that the algorithm does not halt. It's just super slow. You'll have to wait for minutes.

pa = 0
for x in range(1, 10000):
    if x % 100 == 0:
        print(x)
    for y in range(x, 10000):
        m = x*y
        if str(m) == str(m)[::-1]:
            if m > pa:
                pa = m
print(pa)

I changed the second range(1, 10000) to range(x, 10000) to eliminate duplicates like 1*2 and 2*1 into a single 1*2.

If you want a speedup, consider switching to C or C++.

Also you can reverse the iteration order for an extreme speedup

for x in range(10000, 0, -1):
    for y in range(10000, 0, -1):
        m = x*y
        if str(m) == str(m)[::-1]:
            print(m)
            exit(0)
Hoblovski
  • 328
  • 1
  • 9
  • sorry my bad...it was actually the same level as mult = x * y. I put it wrong, and I've edited it – G. Hally Nov 02 '18 at 02:57
  • You cut the time in half with this code, with my earlier code it took around 115 seconds, but with your code, it only took 61 seconds, thank you so much. – G. Hally Nov 02 '18 at 03:04
0

It's kinda an infinite loop, you know, it's very long...

But actually somehow (maybe my computer is fast :-)) the code ran around 15 seconds only...

So it's good.

No halt in it, it's just slow.

U13-Forward
  • 69,221
  • 14
  • 89
  • 114
  • 2
    This code does not cover all the circumstances the OP wants to consider. You code only test like 1*1, 2*2, 3*3 .... The OP wants to test 1*1, 1*2, 1*3 ... 2*1, 2*2, 2*3.... 10000*1, 10000*2 ... 10000*10000. Using zip, you only tested 0.01% of all the situations. – Hoblovski Nov 02 '18 at 02:39
  • @Hoblovski Done! – U13-Forward Nov 02 '18 at 02:42