File tree 1 file changed +48
-0
lines changed
1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change
1
+ using System ;
2
+ namespace _2 . _Even_Fibonacci_numbers
3
+ {
4
+ class EvenFiboNumbers
5
+ {
6
+ static int evenFibSum ( int limit )
7
+ {
8
+ if ( limit < 2 )
9
+ return 0 ;
10
+
11
+ // Initialize first two even
12
+ // prime numbers and their sum
13
+ long ef1 = 0 , ef2 = 2 ;
14
+ long sum = ef1 + ef2 ;
15
+
16
+ // calculating sum of even
17
+ // Fibonacci value
18
+ while ( ef2 <= limit )
19
+ {
20
+
21
+ // get next even value of
22
+ // Fibonacci sequence
23
+ long ef3 = 4 * ef2 + ef1 ;
24
+
25
+ // If we go beyond limit,
26
+ // we break loop
27
+ if ( ef3 > limit )
28
+ break ;
29
+
30
+ // Move to next even number
31
+ // and update sum
32
+ ef1 = ef2 ;
33
+ ef2 = ef3 ;
34
+ sum += ef2 ;
35
+ }
36
+
37
+ return ( int ) sum ;
38
+ }
39
+
40
+ // Driver code
41
+ public static void Main ( )
42
+ {
43
+ int limit = 4000000 ;
44
+ Console . Write ( evenFibSum ( limit ) ) ;
45
+
46
+ }
47
+ }
48
+ }
You can’t perform that action at this time.
0 commit comments