On my way of studying Python i stacked on the problem:
I have to count all numbers from 0 to 130000 which are palindromes, but they should be palindromes after sum of the number and its reversed version. For example:
i
= 93
93 + 39 = 132
132 + 231 = 363
count_of_palindromes
should count all i
which are palindromes after 1 or more iterations of summing, but less than 50.
I've already written the code but it's too way difficult to run on my PC, Python stops responding :(
Any advices how to optimize my code?
count_of_palindromes = 0
for i in range(0,130000):
trying = 0
if (i == int(str(i)[::-1])) != True:
trying += 1
sum = i + int(str(i)[::-1])
while trying <= 50:
while (sum == int(str(sum)[::-1])) != True:
trying += 1
sum += int(str(sum)[::-1])
if trying > 0 and trying <= 50:
count_of_palindromes += 1
break
print(count_of_palindromes)
Thank you very much!