The Keith sequence of an $$n$$-digit ($$n > 1$$) integer $$k$$ ($$k > 9$$) is an integer sequence whose first $$n$$ integers are the $$n$$ digits of $$k$$. So the first three integers in the Keith sequence of 197 are 1, 9 and 7. Each subsequent integer in the sequence is the sum of the previous $$n$$ integers in the sequence. So the subsequent integers in the Keith sequence of 197 are \[ \begin{eqnarray*} 1 + 9 + 7 &=& 17 \\ 9 + 7 + 17 &=& 33 \\ 7 + 17 + 33 &=& 57 \\ 17 + 33 + 57 &=& 107 \\ 33 + 57 + 107 &=& 197 \\ &\vdots & \end{eqnarray*} \] As a result, the Keith sequence of 197 starts as

1, 9, 7, 17, 33, 57, 107, 197, 361, 665, 1223, …

Any Keith sequence is strictly increasing, except for its first $$n$$ integers.

A Keith number is an $$n$$-digit ($$n > 1$$) integer $$k$$ ($$k > 9$$) that occurs in its own Keith sequence. The integer 197 is a Keith number because 197 occurs in the Keith sequence of 197 (indicated in blue above).

The reversal $$\bar{k}$$ of an $$n$$-digit ($$n > 0$$) integer $$k = c_1c_2\ldots c_{n-1}c_n$$ ($$k \geq 0$$) is the integer obtained by inverting the order of its digits: $$\bar{k} = c_nc_{n-1}\ldots c_2c_1$$.

A reverse Keith number is an $$n$$-digit ($$n > 1$$) integer $$k$$ ($$k > 9$$) whose reversal occurs in the Keith sequence of $$k$$. The integer 197 is no reverse Keith number because 791 does not occur in the Keith sequence of 197. The integer 341, on the other hand, is a reverse Keith number because 143 occurs in the Keith sequence of 341

3, 4, 1, 8, 13, 22, 43, 78, 143, 264, 485, 892, 1641, …

Assignment

Example

>>> numbers = [1, 9, 7]
>>> keith_step(numbers)
[9, 7, 17]
>>> numbers
[9, 7, 17]
>>> keith_step(numbers)
[7, 17, 33]
>>> keith_step(numbers)
[17, 33, 57]
>>> keith_step(numbers)
[33, 57, 107]
>>> keith_step(numbers)
[57, 107, 197]
>>> numbers
[57, 107, 197]

>>> keith_sequence(3)
[3]
>>> keith_sequence(11)
[8, 13]
>>> keith_sequence(34)
[29, 47]
>>> keith_sequence(197)
[57, 107, 197]
>>> keith_sequence(1104, target=7000)
[1104, 2128, 4102, 7907]
>>> keith_sequence(3684, target=10000)
[1910, 3684, 7100, 13685]

>>> iskeith(3)
False
>>> iskeith(34, reverse=False)
False
>>> iskeith(197)
True
>>> iskeith(11)
False
>>> iskeith(2580, False)
True
>>> iskeith(86935)
True
>>> iskeith(174680)
True
>>> iskeith(5752090994058710841670361653731519, reverse=False)
True
>>> iskeith(9, True)
False
>>> iskeith(11, reverse=True)
False
>>> iskeith(12, True)
True
>>> iskeith(341, reverse=True)
True
>>> iskeith(5532, True)
True
>>> iskeith(5426705064, reverse=True)
True

Epilogue

No general technique for finding Keith numbers is known, except brute force1 scanning all numbers to check which ones are Keith numbers. Keith numbers seem to be much rarer that primes: there are only 84 Keith numbers having less than 26 digits. We also don't known if there are a finite or an infinite number of Keith numbers. So far, all we knew was that there are 95 Keith numbers with at most 34 digits, with no other Keith numbers known.

$$n$$ count $$n$$-digit Keith numbers
2 6 14, 19, 28, 47, 61, 75
3 2 197, 742
4 9 1104, 1537, 2208, 2580, 3684, 4788, 7385, 7647, 7909
5 7 31331, 34285, 34348, 55604, 62662, 86935, 93993
6 10 120284, 129106, 147640, 156146, 174680, 183186, 298320, 355419, 694280, 925993
7 2 1084051, 7913837
8 3 11436171, 33445755, 44121607
9 2 129572008, 251133297
10 0 (geen)
11 1 24769286411 96189170155
12 4 171570159070, 202366307758, 239143607789, 296658839738
13 2 1934197506555, 8756963649152
14 3 43520999798747, 74596893730427, 97295849958669
15 3 120984833091531, 270585509032586, 754788753590897
16 3 3621344088074041, 3756915124022254, 4362827422508274
17 5 11812665388886672, 14508137312404344, 16402582054271374, 69953250322018194, 73583709853303061
18 3 119115440241433462, 166308721919462318, 301273478581322148
19 5 1362353777290081176, 3389041747878384662, 5710594497265802190, 5776750370944624064, 6195637556095764016
20 3 12763314479461384279, 27847652577905793413, 45419266414495601903
21 1 855191324330802397989
22 1 7657230882259548723593
23 3 26842994422637112523337, 36899277593852609997403, 61333853602129819189668
24 1 229146413136585558461227
25 1 9838678687915198599200604
26 3 18354972585225358067718266, 19876234926457288511947945, 98938191214220718050301312
27 7 153669354455482560987178342, 154677881401007799974564336, 133118411174059688391045955, 154140275428339949899922650, 295768237361291708645227474, 956633720464114515890318410, 988242310393860390066911414
28 1 9493976840390265868522067200
29 2 41796205765147426974704791528, 70267375510207885242218837404
30 5 127304146123884420932123248317, 389939548933846065763772833753, 344669719564188054170496150677, 756672276587447504826932994366, 534139807526361917710268232010
31 2 1598187483427964679092074853838, 2405620130870553672640058975437
32 4 41030306579725050560909919549414, 47824404246899742508216679149392, 42983394195992223818343905028410, 89980815134051887612993101615858
33 6 172451142646837728336412943204299, 193962639439026709638083447831059, 381933008901296879565658130750756, 359253598248137147666007355623218, 303294117104027490007126494842828, 312736110821858321305917486145434
34 3 1876178467884883559985053635963437, 2787674840304510129398176411111966, 5752090994058710841670361653731519

In 2004, D. Lichtblau found the 26-digit Keith number 98938191214220718050301312 using integer linear programming2 to solve the relevant Diophantine equations3. He also found all 30- and 31-digit Keith numbers on June 23, 2009 and all 32-, 33- and 34-digit Keith numbers on August 26, 2009. This made the 34-digit integer 5752090994058710841670361653731519 the largest integer known so far, because no new Keith numbers have been found since August 2009.

However, when compiling this programming assignment in December 2022, Ghent University (Belgium) mathemagician Toon Baeyens succeeded in further expanding the list of known Keith numbers with all 35- and 36-digit Keith numbers.

$$n$$ count $$n$$-digit Keith numbers
35 4 23137274755282109912063039769168142, 25314398891465125143523864790391288, 44715370344837755402179510861188022, 47933465320021485928519060435917729
36 5 196866601633638871239614307772203156, 214860400509971669129437189647933258, 394684240118372710589383926683340073, 763701584467955209221750616718219223, 880430656963418264331749765271577784