dc, 25 22 bytes
9k5v1+2/3?*1-^5v/0k2/p
Try it online!
Or save the program in a file and run it by typing
dc -f *filename*
The program accepts a non-negative integer n on stdin, and it outputs the sum of the first n even Fibonacci numbers on stdout. (The Fibonacci sequence is taken to start with 0, as per the OP's examples.)
This program uses the formula (F(3n-1)-1)/2 for the sum of the first n even Fibonacci numbers, where F is the usual Fibonacci function, given by F(0) = 0, F(1) = 1, F(n) = F(n-2) + F(n-1) for n >= 2.
dc is a stack-based calculator. Here's a detailed explanation:
9k # Sets the precision to 9 decimal places (which is more than sufficient).
5v # Push the square root of 5
1+ # Add 1 to the number at the top of the stack.
2/ # Divide the number at the top of the stack by 2.
At this point, the number (1+sqrt(5))/2 is at the top of the stack.
3 # Push 3 on top of the stack.
? # Read a number from stdin, and push it.
\* # Pop two numbers from the stack, multiply them, and push the product
1- # Subtract 1 from the number at the top of the stack.
At this point, 3n-1 is at the top of the stack (where n is the input), and (1+sqrt(5))/2 is second from the top.
^ # Pop two numbers from the stack (x, then y), compute the power y^x, and push that back on the stack.
5v/ # Divide the top of the stack by sqrt(5).
At this point, the number at the top of the stack is (((1+sqrt(5))/2)^(3n-1))/sqrt(5). The closest integer to this number is F(3n-1). Note that F(3n-1) is always an odd number.
0k # Change precision to 0 decimal places.
2/ # Divide the top of the stack by 2, truncating to an integer.
p # Print the top of the stack on stdout.
1https://oeis.org/A099919 – xnor – 2017-01-03T23:42:34.493
@EasterlyIrk The test cases imply the latter, but it should be explicitly stated. – Mego – 2017-01-03T23:49:19.510
@Mego yeah, I figured as much. – Rɪᴋᴇʀ – 2017-01-03T23:49:33.757
@EasterlyIrk, Mego is right. It is the latter. – dfernan – 2017-01-03T23:58:11.513
9Please don't accept answers so fast. It's only been an hour, golfier answer could come in. EDIT: I see now there's already a shorter answer that's not accepted yet. – Rɪᴋᴇʀ – 2017-01-04T00:42:44.903
6It's customary to wait at least a week before accepting an answer, because many people interpret it as a sign that the challenge is no longer active. – Zgarb – 2017-01-04T07:54:18.300
Shouldn't it be
n(1) = 0
? Your statementn = 0
makes no sense. – devRicher – 2017-01-04T18:54:20.630@devRicher I have corrected that. Someone else added that statement to my original post and I did not spot the mistake. – dfernan – 2017-01-04T19:43:57.703