Pentabonacci Numbers
December 13, 2019
You don’t need a computer program to figure out which members of the sequence are odd: the sequence starts with an even number, then the pattern two odds and four evens continues indefinitely, so the nth member of the sequence is odd when n (mod 6) ∈ {2, 3}. And our Standard Prelude makes it easy to calculate the sequence:
> (iterate 30 + 0 1 1 2 4) (0 1 1 2 4 8 16 31 61 120 236 464 912 1793 3525 6930 13624 26784 52656 103519 203513 400096 786568 1546352 3040048 5976577 11749641 23099186 45411804 89277256)
You can run the program at https://ideone.com/i3yHrt.
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: