-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheuler2.cpp
More file actions
73 lines (49 loc) · 1.33 KB
/
Copy patheuler2.cpp
File metadata and controls
73 lines (49 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//
// Created by Henry Glover on 8/16/23.
//
//
// fibonacci sequence - add two previous terms
//1,2,3,5,8,13,21,34,55,89
// By considering the terms in the Fibonacci sequence whose values do
// not exceed four million, find the sum of the even-valued terms.
#include <iostream>
// function declaration
void euler2 (int &fibSumEven) ;
int main ()
{
int fibSumEven = 0;
euler2(fibSumEven);
std::cout << "Sum of Even Fibonacci values below 4 million is : " << fibSumEven << std::endl;
return 0;
}
// function definition
void euler2 (int &fibSumEven)
{
int fibA = 1;
int fibB = 2;
int fibC = 0;
while (fibB < 4000000)
{
fibC = fibA + fibB;
fibA = fibB;
fibB = fibC;
if (fibC % 2 == 0)
{
fibSumEven += fibC;
std::cout << "Added to sum even value : " << fibC << std::endl;
}
}
}
/* this was how I first solved it ::
for (int i = 0; fibB < 4000000 && fibA < 4000000; ++i) {
fibC = fibA + fibB;
fibA = fibB;
fibB = fibC;
if (fibC % 2 == 0) {
fibSumEven = fibSumEven + fibC;
std::cout << "added to sum even vale : " << fibC << std::endl;
}
}
std::cout << " Sum of Even Fibonachi values below 4 Million is : " << fibSumEven << std::endl;
}
*/