License Public Domain
Lines 46
Permissions
Owner: Stou S.
Group Owner: SnortSnort
Viewable by Everyone
Editable by All Siafoo Users
Hide
Meet people who work on similar things as you – get help if you need it Join Siafoo Now or Learn More

Euler Project Problem 2 Atom Feed 0

# 's
 1#include <iostream>
2
3/*
4 * Contrived operator overloaded solution
5 */
6struct fib{
7
8 int l, c, position;
9
10 fib() : l(1), c(1), position(0) {}
11
12 void operator++() {
13 int tmp = c;
14 c = l + c;
15 l = tmp;
16 ++position;
17 }
18
19 int get(){
20 return c;
21 }
22
23};
24
25/*
26 * Recursive solution
27 */
28int fib_r(int p, int c, int limit){
29
30 if(p + c >= limit){
31 return 0;
32 }
33
34 if((p + c) % 2 == 0){
35 return p + c + fib_r(c, p + c, limit);
36 }else{
37 return fib_r(c, p + c, limit);
38 }
39}
40
41int fib_i(){
42 int sum = 0;
43
44 for(fib f; f.get() <= 4e6; ++f){
45 if(f.get() % 2 == 0){
46 sum += f.get();
47 }
48 }
49 return sum;
50}
51
52int main(){
53
54 // Run iterative solution
55 std::cout<<fib_i()<<std::endl;
56
57 // Run recursive solution
58 std::cout<<fib_r(1, 1, 4e6)<<std::endl;
59
60 return 0;
61}