Pentabonacci Numbers
December 13, 2019
The sequence of pentabonacci numbers is defined as the sequence beginning 0, 1, 1, 2, 4, and continuing with each subsequent number the sum of the five previous members of the sequence. The sequence grows very quickly; the 20th member of the sequence (counting the 0 that begins the sequence as the first member) is 103519.
Your task is to write a program that calculates the first n members of the pentabonacci sequence and determine the rule that states whether the nth member of the sequence is even or odd. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.
Klong version
My implementation in Julia: https://pastebin.com/c6CEEJ2e. Note that the BigInt type is preferred as the numbers in the sequence soon grow beyond what Int128 can handle.
Using the map(isodd, p) command, for a large enough array p containing a series of Pentabonacci numbers, it becomes evident that the pattern that these numbers follow is: O,O,E,E,E,E (O = odd, E = even), starting from the 2nd number onwards.
Have a nice weekend!
http://mathworld.wolfram.com/PentanacciNumber.html
That Haskell is using the generating function from Mathworld and polynomial division to generate the sequence (and the Fibonacci numbers as well).
Matthew’s Haskell solution made me want to shorten mine:
Klong version
@Steve: very nice, but any chance of an explanation what that code is doing?
@matthew – Here you go. Thanks for asking
Would you mind doing this for your Haskell example?
Thanks, great explanation. Here’s some light on the Haskell:
For some reason, WordPress won’t accept the output for that code as a comment – as you can guess, it’s the Fibonacci, Tribonacci, etc. numbers, of course.
More comments on the code itself:
Here’s a solution in Python with a function for calculating the mth Fibonacci n-step number. n is 5 for the Pentanacci numbers.
Output:
My solution calculates the first m numbers. I mistakenly wrote that wrong in my last post, which suggested the function only returns the mth number alone.
Made a change and an addition to my explanation for the current Klong solution (See entries marked with ***):
Inspired by Daniel’s solution, here’s a variation using Python generators – this one omits the 0 at the start of the sequences:
Here’s a Python function for calculating the parity of the mth Fibonacci n-step number, returning 0 for evens and 1 for odds.
Output: