The Lucas-nacci Numbers
Jelly, 12 bytes
;2U+¥Ð¡-,1ZḢ
Try it online!
Background
We can extend F and L to -1 by defining F(-1) = 1 and L(-1) = -1. This is consistent with the recursive function.
Our program starts with
-1 1
0 2
In each step, to form the next pair, we reverse the last pair and add it to the penultimate pair. For example:
[0, 2] U+¥ [-1, 1] -> [2, 0] + [-1, 1] -> [1, 1]
If we continue this process for a few more steps, we get
-1 1
0 2
1 1
1 3
4 2
3 7
11 5
The Lucas-nacci sequence is simply the left column.
How it works
;2U+¥Ð¡-,1ZḢ Niladic link. No implicit input.
Since the link doesn't start with a nilad, the argument 0 is used.
;2 Concatenate the argument with 2, yielding [0, 2].
-,1 Yield [-1, 1]. This is [L(-1), F(-1)].
¥ Create a dyadic chain of the two atoms to the left:
U Reverse the left argument.
+ Add the reversed left argument and the right one, element-wise.
С For reasons‡, read a number n from STDIN.
Repeatedly call the dyadic link U+¥, updating the right argument with
the value of the left one, and the left one with the return value.
Collect all intermediate results.
Z Zip the list of results, grouping the first and seconds coordinates.
Ḣ Head; select the list of first coordinates.
‡ С
peeks at the two links to the left: 2
and U+¥
. Since the leftmost one is a nilad, it cannot be the body of the loop. Therefore, U+¥
is used as body and a number is read from input. Since there are no command-line arguments, that number is read from STDIN.
Haskell, 59, 57, 56, 52, 51 bytes
l a=2*mod a 2:scanl(+)1(l a)
f n=[l i!!i|i<-[0..n]]
Series definition adapted from this answer.
Less golfed:
fibLike start = start : scanl (+) 1 (fibLike start)
whichStart i = (2*mod i 2)
lucasNacci i = fibLike (whichStart i) !! i
firstN n = [ lucasNacci i | i <- [0..n]]
fibLike start
gives an infinite list, defined: f(0)=start, f(1)=1, f(n)=f(n-1) + f(n-2)
.
whichStart i
returns 2 for odd input (Lucas series) or 0 for even (Fibonacci series).
lucasNacci i
gives the ith Lucas-nacci number.
firstN n
maps over the list.
One byte saved by Boomerang.
CJam, 21 20 bytes
Thanks to Sp3000 for saving 1 byte.
TXX4ri{1$3*4$-}*?;]p
Test it here.
Explanation
Simply uses the recurrence given in the challenge spec.
TXX4 e# Push 0 1 1 4 as base cases.
ri e# Read input and convert to integer N.
{ e# Run this N times...
1$ e# Copy a(n-2).
3* e# Multiply by 3.
4$ e# Copy a(n-4).
- e# Subtract.
}*
?; e# Discard the last three values, using a ternary operation and popping the result.
]p e# Wrap the rest in an array and pretty-print it.