#K6926. Even Fibonacci Sum
Even Fibonacci Sum
Even Fibonacci Sum
You are given an integer n
. Your task is to compute the sum of all even Fibonacci numbers that do not exceed n
. The Fibonacci sequence is defined as:
\(F_0 = 0,\; F_1 = 1\) and \(F_{k+2} = F_{k+1} + F_k\) for \(k \ge 0\).
For example, when \(n = 34\), the Fibonacci sequence terms that do not exceed 34 are:
- 0 (even)
- 1
- 1
- 2 (even)
- 3
- 5
- 8 (even)
- 13
- 21
- 34 (even)
The sum of the even-valued terms is \(0+2+8+34 = 44\). Your solution should read the input from standard input and print the result to standard output.
inputFormat
The input consists of a single line containing one integer n
(\(0 \le n \le 4\,000\,000\)).
outputFormat
Output a single integer, which is the sum of the even Fibonacci numbers that do not exceed n
.
10
10