Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8827519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:23:25+00:00 2026-06-14T07:23:25+00:00

what I am trying to do: If the user specifies return_length=True when calling your

  • 0

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..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T07:23:27+00:00Added an answer on June 14, 2026 at 7:23 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Super short version: I'm trying to use a user-defined function to populate a new
I hope someone can help with the following. I'm trying to create a user-specific
So, I'm trying to pull all photos of a specific user's account via the
I trying authenticate user in Spring Security application via oAuth. I'm already received token
I'm currently trying to user actionlink helpers in a way that I don't think
I am trying retrieve user name from the code through graph api, the below
I am a noob Perl user trying to get my work done ASAP so
I'm a Git user trying to use Mercurial. Here's what happened: I did a
Trying to alert user when internet is unavailable (and retry when they dismiss message).
I'm trying to authenticate user accounts in Windows Phone. I found this C# library

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.