Python script for finding keith numbers not working
<p>I am trying to make a python program that will find keith numbers. If you don't what keith numbers are, here is a link explaining them:
<a href="http://mathworld.wolfram.com/KeithNumber.html" rel="nofollow">Keith Numbers - Wolfram MathWorld</a></p>

<p>My code is</p>

<pre><code>from decimal import Decimal
from time import sleep

activator1 = 1

while (activator1 == 1):
    try:
        limit = int(raw_input("How many digits do you want me to stop at?"))
        activator1 = 0
    except ValueError:
        print "You did not enter an integer"


limitlist = []
activator2 = 1

while (activator2 &lt;= limit):
    limitlist.append(activator2)
    activator2 += 1
    print limitlist

add1 = 0
add = 0
count = 9
while 1:
    sleep (0.1)


    numbers = list(str(count))

    for i in limitlist:
        if (i &gt; 0) &amp; (add &lt; count):
            add = sum(Decimal(i) for i in numbers)
            lastnumber = int(numbers[-1])
            add1 = lastnumber+int(add)
            numbers.reverse()
            numbers.pop()
            numbers.append(add1)
            print add1
            print add
            print count
            print numbers
        if (add1 == count):
            print"________________________________"
            print add1
            print count

        elif (i &gt; 0) &amp; (add &gt; count):
            count += 1
            break
</code></pre>

<p>It doesn't output any errors but it just outputs </p>

<pre><code>18
9
9
[18]
</code></pre>

<p>Could someone please tell me why it doesn't just repeatedly find Keith numbers within the number of integers range? </p>

<p>Prune has already given you good advices! Let's put a little example of what he meant though, let's say you got an algorithm which determine whether n is a keith number or not and also a test loop to print some keith numbers:</p>

<pre><code>def keith_number(n):
    c = str(n)
    a = list(map(int, c))
    b = sum(a)

    while b &lt; n:
        a = a[1:] + [b]
        b = sum(a)

    return (b == n) &amp; (len(c) &gt; 1)

N = 5
for i in range(N):
    a, b = 10**i, 10**(i + 1)
    print("[{0},{1}]".format(a, b))
    print([i for i in filter(keith_number, range(a, b))])
    print('-' * 80)
</code></pre>

<p>such snippet gives you this:</p>

<pre><code>[1,10]
[]
--------------------------------------------------------------------------------
[10,100]
[14, 19, 28, 47, 61, 75]
--------------------------------------------------------------------------------
[100,1000]
[197, 742]
--------------------------------------------------------------------------------
[1000,10000]
[1104, 1537, 2208, 2580, 3684, 4788, 7385, 7647, 7909]
--------------------------------------------------------------------------------
[10000,100000]
[31331, 34285, 34348, 55604, 62662, 86935, 93993]
--------------------------------------------------------------------------------
</code></pre>

<p>Wow, that's awesome... but wait, let's say you don't understand the keith_number function and you want to explore a little bit the algorithm in order to understand its guts. What about if we add some useful debug lines?</p>

<pre><code>def keith_number(n):
    c = str(n)
    a = list(map(int, c))
    b = sum(a)
    print("{0} = {1}".format("+".join(map(str, a)), b))

    while b &lt; n:
        a = a[1:] + [b]
        b = sum(a)
        print("{0} = {1}".format("+".join(map(str, a)), b))

    return (b == n) &amp; (len(c) &gt; 1)

keith_number(14)
print '-' * 80
keith_number(15)
</code></pre>

<p>that way you'll be able to trace the important steps and the algorithm will make sense in your head:</p>

<pre><code>1+4 = 5
4+5 = 9
5+9 = 14
--------------------------------------------------------------------------------
1+5 = 6
5+6 = 11
6+11 = 17
</code></pre>

<p>Conclusion: I'd advice you learn how to debug your own code instead asking strangers about it ;-)</p>

python python-2.7 math numbers