Note that there are some explanatory texts on larger screens.

plurals
  1. PO196 algorithm with optional inputs in function
    text
    copied!<p>what I am trying to do:</p> <ol> <li><p>If the user specifies <code>return_length=True</code> 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 <code>return_length=True</code>, 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.</p></li> <li><p>If the user did not specify <code>return_length</code> or specified <code>return_length=False</code>, 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.</p></li> </ol> <p>Some background on the 196 algorithm:</p> <p>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 <a href="http://mathworld.wolfram.com/196-Algorithm.html" rel="nofollow">http://mathworld.wolfram.com/196-Algorithm.html</a>)</p> <p>What I have so far:</p> <pre><code>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 </code></pre> <p>I get the error:</p> <pre><code>Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; File "_sage_input_36.py", line 10, in &lt;module&gt; exec compile(u"print _support_.syseval(python, u'alg196(34)', __SAGE_TMP_DIR__)" + '\n', '', 'single') File "", line 1, in &lt;module&gt; 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 &lt;module&gt; File "", line 3, in alg196 TypeError: 'int' object has no attribute '__getitem__' </code></pre> <p>I am not sure how to fix this problem or the error exactly.</p> <p>Taking some of the information for the answers i have this new code:</p> <pre><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 </code></pre> <p>But is still do not get the palindromic number for or the sequence to the palindromic number..</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload