Test if given number is a Keith number
Python (78 75)
a=input()
n=map(int,`a`)
while a>n[0]:n=n[1:]+[sum(n)]
print(a==n[0])&(a>9)
n=n[1:]+[sum(n)]
does all the magic. It takes every item but the first item of n
, sticks on the sum of n
(with the first item), then sets that to n
.
I wish you could call list
on an integer and have the digits seperated.
Returns False
on all inputs below 10. Can be 8 characters shorter if it returned True
.
GolfScript (31 25 chars)
..[10base{.{+}*+(\}@*]?0>
Input as an integer on top of the stack. Output is 0 (false) or 1 (true). Online demo which lists the Keith numbers up to 100.
GolfScript, 32 29 characters
...[10base\{.{+}*+(\}*]&,\9>&
A GolfScript implementation which can be tested online. Input is given as top element on the stack and it returns 0 (i.e. false) or 1 respectively.