3
votes

what I am trying to do:

  1. If the user specifies return_length=True when calling your function, it should return one plus the number of steps the algorithm required to reach a palindromic number. For example, with an input of 5280 and return_length=True, your function should return 4 (Note that this is the total number of entries in the sequence [5280, 6105, 11121, 23232]). With an input of 11, for example, the function should return 1 because it is already a palindromic number.

  2. If the user did not specify return_length or specified return_length=False, your function should return the palindromic number at which the algorithm terminates. For example, with an input of 5280, the algorithm should return 23232 (an integer, not a string). Similarly, with an input of 89, it should return the integer 8813200023188.

Some background on the 196 algorithm:

Take any positive integer of two digits or more, reverse the digits, and add to the original number. This is the operation of the reverse-then-add sequence. Now repeat the procedure with the sum so obtained until a palindromic number is obtained. This procedure quickly produces palindromic numbers for most integers. For example, starting with the number 5280 produces the sequence 5280, 6105, 11121, 23232. The end results of applying the algorithm to 1, 2, 3, ... are 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 11, 33, 44, 55, 66, 77, 88, 99, 121, ... (Sloane's A033865). The value for 89 is especially large, being 8813200023188. (from http://mathworld.wolfram.com/196-Algorithm.html)

What I have so far:

def alg196(x, y = false):
   if y==False:
      while x == x[::-1]:
         x==x+x[::-1]
      return x
   else:
      seq = [x]
      while x == x[::-1]:
         x==x+x[::-1]
      seq.append(x)
   return seq

I get the error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "_sage_input_36.py", line 10, in <module>
    exec compile(u"print _support_.syseval(python, u'alg196(34)', __SAGE_TMP_DIR__)" + '\n', '', 'single')
  File "", line 1, in <module>

  File "/sagenb/sage_install/sage-5.3-sage.math.washington.edu-x86_64-Linux/devel/sagenb-git/sagenb/misc/support.py", line 487, in syseval
    return system.eval(cmd, sage_globals, locals = sage_globals)
  File "/sagenb/sage_install/sage-5.3-sage.math.washington.edu-x86_64-Linux/local/lib/python2.7/site-packages/sage/misc/python.py", line 56, in eval
    eval(z, globals)
  File "", line 1, in <module>

  File "", line 3, in alg196

TypeError: 'int' object has no attribute '__getitem__'

I am not sure how to fix this problem or the error exactly.

Taking some of the information for the answers i have this new code:

    def alg196(x, y = false):
if y==False:
    while str(x) == str(x)[::-1]:
       x=str(x)+str(x)[::-1]
    return x
else:
    seq = [x]
    while str(x) == str(x)[::-1]:
        x = str(x)+str(x)[::-1]
    seq.append(x)
return seq

But is still do not get the palindromic number for or the sequence to the palindromic number..

3
basically it makes a palindromic number and either returns the palindrome or the sequence to get to the palindromic number. - user1698174

3 Answers

4
votes

x[::-1] does not work on a number:

>>> 42[::-1]
TypeError: 'int' object has no attribute '__getitem__'

You need to convert it to a string, reverse it, then convert it back to an int:

>>> int(str(42)[::-1])
24

Secondly, the line

x==x+x[::-1]

does absolutely nothing. Don't confuse = and ==

2
votes

something like this:

def algo(r,ret_len=None):
   count=0
   while 1:
      r=str(r)
      if r==r[::-1]:
         break
      else:
         count+=1
         r=int(r)+int(r[::-1])
   return count+1 if ret_len else r

print (algo(5280,True))
print (algo(5280))
print (algo(89,True))
print (algo(89))

output:

4
23232
25
8813200023188
1
votes

You can't get a 'slice' of an integer. You need to convert it to a string, first. x[::-1] <-- this operation is illegal if x is an integer.

Incidentally, this would be better written as a few functions-- functions that perform the calculations, and a function that runs functions while under a certain constraint, and takes in the return_length=True argument.

def reverse(n):
    return int(str(n)[::-1])

def is_palindrome(n):
    return str(n) == str(n)[::-1]

def alg196(n, return_length=False):
    results = [n]
    while not is_palindrome(results[-1]):
        results.append(n + reverse(n))
        n = results[-1]
    return results[-1] if not return_length else len(results)

edit

A slightly quicker variation based on Ashwini Chaudhary's code. The above version produces a list of results, it's nice if you want to do something with the intermediate numbers. But, I think the following function is the best compromise between readability and speed. I have no idea why he starts with count=0 though.

def alg196(n, return_length=False):
    count = 1
    while not is_palindrome(n):
        n = n + reverse(n)
        count += 1
    return count if return_length else n