I learned about this amazing sequence recently. It starts, 1, 12, 35, 94…. with next term found by concatenated the final digit of the previous term with the first digit of this new term (to form a 2 digit number) and adding it on to the previous term.
Eg: …12, 35…. the final digit of 12 is 2, the first digit of 35 is 3. These concatenated form 23, and that’s what’s added to 12 to get to 35.
More information about the sequence can be found here.
def generateSequence(starting_num, n):
a, y = starting_num, 1
result = []
while len(result) < n:
result.append(a)
a, y = a + 10 * (a % 10), 1
while y < 10:
if str(a + y)[0] == str(y):
a += y
break
y += 1
return result
terms = generateSequence(1,100)
print(terms)
It gives an output like this:
[1, 12, 35, 94, 135, 186, 248, 331, 344, 387, 461, 475, 530, 535, 590, 595, 651, 667, 744, 791, 809, 908, 997, 1068, 1149, 1240, 1241, 1252, 1273, 1304, 1345, 1396, 1457, 1528, 1609, 1700, 1701, 1712, 1733, 1764, 1805, 1856, 1917, 1988, 2070, 2072, 2094, 2136, 2198, 2280, 2282, 2304, 2346, 2408, 2490, 2492, 2514, 2556, 2618, 2700, 2702, 2724, 2766, 2828, 2910, 2912, 2934, 2976, 3039, 3132, 3155, 3208, 3291, 3304, 3347, 3420, 3423, 3456, 3519, 3612, 3635, 3688, 3771, 3784, 3827, 3900, 3903, 3936, 3999, 4093, 4127, 4201, 4215, 4269, 4363, 4397, 4471, 4485, 4539, 4633]
With a bit of modification we can explore when the sequence fails:
def generateSequence(starting_num):
result = []
a, y = starting_num, 1
position = 0
while True:
result.append(a)
a, y = a + 10 * (a % 10), 1
position += 1
found_next = False
while y < 10:
if str(a + y)[0] == str(y):
a += y
found_next = True
break
y += 1
if not found_next:
break
return result[-1], position
final_term, position = generateSequence(1)
print(f"The final term is {final_term}, and it is at position {position} in the sequence.")
The final term is 99999945, and it is at position 2137453 in the sequence.