Mar-06-2021, 12:44 PM
I played a little with your gen_fib, letting start to always be 1 and to generate any of the generalized fib sequences (shortened the name to gen_ib, but ...)
def gen_ib(length, n): '''Function to generate the n-th number sequence generalization of the Fibonacci number sequence. n == 2 => Fibonacci number sequence n == 3 => "Tribonacci" number sequence n == 4 => "Tetranacci" number sequence and so on ...''' if n < 2: return [] buffer = [0]*(n-1)+[1] if length <= n: return buffer[:length] b = 0 for j in range(length-n): buffer.append(sum(buffer[b:])) b += 1 return(buffer)Then I tested it with
for i in range(2,10): print(gen_ib(20+i, i))and it works fine.
Output:[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946]
[0, 0, 1, 1, 2, 4, 7, 13, 24, 44, 81, 149, 274, 504, 927, 1705, 3136, 5768, 10609, 19513, 35890, 66012, 121415]
[0, 0, 0, 1, 1, 2, 4, 8, 15, 29, 56, 108, 208, 401, 773, 1490, 2872, 5536, 10671, 20569, 39648, 76424, 147312, 283953]
[0, 0, 0, 0, 1, 1, 2, 4, 8, 16, 31, 61, 120, 236, 464, 912, 1793, 3525, 6930, 13624, 26784, 52656, 103519, 203513, 400096]
[0, 0, 0, 0, 0, 1, 1, 2, 4, 8, 16, 32, 63, 125, 248, 492, 976, 1936, 3840, 7617, 15109, 29970, 59448, 117920, 233904, 463968]
[0, 0, 0, 0, 0, 0, 1, 1, 2, 4, 8, 16, 32, 64, 127, 253, 504, 1004, 2000, 3984, 7936, 15808, 31489, 62725, 124946, 248888, 495776]
[0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 4, 8, 16, 32, 64, 128, 255, 509, 1016, 2028, 4048, 8080, 16128, 32192, 64256, 128257, 256005, 510994]
[0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 4, 8, 16, 32, 64, 128, 256, 511, 1021, 2040, 4076, 8144, 16272, 32512, 64960, 129792, 259328, 518145]
Use it, modify it to your taste or just skip it if it is not useful in your line of ideas. I'm just playing around a little when taking a rest from my own work.