Project Euler in C
All Files Functions Macros Pages
Functions
0002.c File Reference

Sum of the even Fibonacci numbers less than or equal to 4000000. More...

#include <stdio.h>

Go to the source code of this file.

Functions

long fib (const int n)
 Computes the nth Fibonacci number, starting with 1 and 2.
 
int main (void)
 

Detailed Description

Sum of the even Fibonacci numbers less than or equal to 4000000.

https://projecteuler.net/problem=2

Definition in file 0002.c.

Function Documentation

◆ fib()

long fib ( const int  n)

Computes the nth Fibonacci number, starting with 1 and 2.

Parameters
nAn integer >= 1.

Definition at line 30 of file 0002.c.

30 {
31 if (n == 1) {
32 return 1;
33 }
34 if (n == 2) {
35 return 2;
36 }
37 return fib(n - 1) + fib(n - 2);
38}
long fib(const int)
Computes the nth Fibonacci number, starting with 1 and 2.
Definition 0002.c:30

◆ main()

int main ( void  )

Definition at line 10 of file 0002.c.

10 {
11 long sum = 0;
12
13 int i;
14 long value;
15 for (i = 1; (value = fib(i)) <= 4000000; i++) {
16 if (value % 2 == 0) {
17 sum += value;
18 }
19 }
20
21 printf("%ld\n", sum);
22
23 return 0;
24}