renzen - 1 year ago 79

Python Question

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:

Keith Numbers - Wolfram MathWorld

My code is

`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 <= 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 > 0) & (add < 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 > 0) & (add > count):

count += 1

break

It doesn't output any errors but it just outputs

`18`

9

9

[18]

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

Answer Source

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:

```
def keith_number(n):
c = str(n)
a = list(map(int, c))
b = sum(a)
while b < n:
a = a[1:] + [b]
b = sum(a)
return (b == n) & (len(c) > 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)
```

such snippet gives you this:

```
[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]
--------------------------------------------------------------------------------
```

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?

```
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 < n:
a = a[1:] + [b]
b = sum(a)
print("{0} = {1}".format("+".join(map(str, a)), b))
return (b == n) & (len(c) > 1)
keith_number(14)
print '-' * 80
keith_number(15)
```

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

```
1+4 = 5
4+5 = 9
5+9 = 14
--------------------------------------------------------------------------------
1+5 = 6
5+6 = 11
6+11 = 17
```

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