Coincidentally, following the previous post, New Scientist’s regular brain teaser featured the iccanobiF Sequence, which is just like the Fibonacci Sequence, except that after adding the two previous numbers the digits of the results are reversed. Obviously the sequences are the same for single digit values, but following 5 and 8 the Fibonacci result is 13, and iccanobiF is 31.
Some examples of code for generating the sequence can be found at geeksforgeeks:
Program to find first N Iccanobif Numbers
The examples in the link all use a loop to find the value of the reversed digits, but in Python this can be done with a single line of code:
@xl_func()
@xl_arg('n', 'int')
def iccanobif(n):
f1 = 1
f2 = 1
for i in range(2, n):
f3 = int(str(f2+f1)[::-1])
f1 = f2
f2 = f3
return f3
The code includes the pyxll decorators to link to Excel. The line generating the numbers in the sequence:
- Adds f1 and f2 (to generate the next Fibonacci number)
- Converts this to a string
- Reverses the string
- Converts the string back to an integer (which will automatically remove any leading zeros)
The results of this function called from Excel are shown below, together with the equivalent Fibonacci numbers.
