Attempting to see whether using 196 as a
respectively in my program will have a result or not, I made a simple function to test it. Now the 196-Algorithm requires this:
Take any positive, two-digit integer with at least two digits,
a
Take
a
and reverse it (for example 23 becomes 32)Now add the new number to
a
Repeat the process until the
a
is a palindrome (when reversed, a palindrome's value should still be the same)
This is my function:
def one_nine_six_algorithm(a):
b = a
if a > 9:
while str(b)[::-1] != str(b):
c = str(b)[::-1]
b += int(c)
return b
In this program, I literally did how do the algorithm. The integer, a
, is taken in and checked that it is a positive, two-digit number. Then, the algorithm is run in the while
loop, where b
is the new/current value of a
and c
is basically b
reversed but as a string in able to use [::-1]
. Then c
as an integer (int(c)
) is added to b
and is continued until b
is a palindrome (checked in the while
line). Then it returns b
, which is used for whatever reason I need it for.
Now for complicated integers, this might go through a lot of loops and take a lot of time to finish. Is there a more efficient way to improve my program in terms of speed and readability?