File tree Expand file tree Collapse file tree 6 files changed +78
-4
lines changed Expand file tree Collapse file tree 6 files changed +78
-4
lines changed Original file line number Diff line number Diff line change 1
- // See <https://stackoverflow.com/questions/156767/whats-the-difference-between-an-argument-and-a-parameter>.
2
-
3
1
#include < iostream>
4
2
5
3
void func (int param) {
Original file line number Diff line number Diff line change 1
- // See <https://stackoverflow.com/questions/156767/whats-the-difference-between-an-argument-and-a-parameter>.
2
-
3
1
#include < iostream>
4
2
5
3
constexpr int fibonacci (int i) {
Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+
3
+ unsigned long long fibonacci (int i) {
4
+ static unsigned long long cache[128 ]{0 , 1 }, next = 2 ;
5
+ for (; next <= i; ++next) {
6
+ cache[next] = cache[next - 1 ] + cache[next - 2 ];
7
+ }
8
+ return cache[i];
9
+ }
10
+
11
+ int main (int argc, char **argv) {
12
+ auto fib100 = fibonacci (100 );
13
+ std::cout << " fibonacci(100) = " << fib100 << std::endl;
14
+ return 0 ;
15
+ }
Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+
3
+ struct Fibonacci {
4
+ size_t cache[128 ];
5
+ int next = 2 ;
6
+
7
+ Fibonacci () : cache{0 , 1 }, next(2 ) {}
8
+ ~Fibonacci () {
9
+ std::cout << " Drop Fibonacci with next = " << next << std::endl;
10
+ }
11
+
12
+ size_t operator [](int i) {
13
+ for (; next <= i; ++next) {
14
+ cache[next] = cache[next - 1 ] + cache[next - 2 ];
15
+ }
16
+ return cache[i];
17
+ }
18
+ };
19
+
20
+ int main (int argc, char **argv) {
21
+ Fibonacci fib0, fib1;
22
+ std::cout << " fibonacci(10) = " << fib0[10 ] << std::endl;
23
+ std::cout << " fibonacci(100) = " << fib1[100 ] << std::endl;
24
+ return 0 ;
25
+ }
Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+
3
+ class Fibonacci {
4
+ size_t cache[128 ];
5
+ int next = 2 ;
6
+
7
+ public:
8
+ Fibonacci () : cache{0 , 1 }, next(2 ) {}
9
+ ~Fibonacci () {
10
+ std::cout << " Drop Fibonacci with next = " << next << std::endl;
11
+ }
12
+
13
+ size_t operator [](int i) {
14
+ for (; next <= i; ++next) {
15
+ cache[next] = cache[next - 1 ] + cache[next - 2 ];
16
+ }
17
+ return cache[i];
18
+ }
19
+ };
20
+
21
+ int main (int argc, char **argv) {
22
+ Fibonacci fib0, fib1;
23
+ std::cout << " fibonacci(10) = " << fib0[10 ] << std::endl;
24
+ std::cout << " fibonacci(100) = " << fib1[100 ] << std::endl;
25
+ return 0 ;
26
+ }
Original file line number Diff line number Diff line change @@ -24,3 +24,15 @@ target("exercise04")
24
24
target (" exercise05" )
25
25
set_kind (" binary" )
26
26
add_files (" exercises/05_constexpr/main.cpp" )
27
+
28
+ target (" exercise06" )
29
+ set_kind (" binary" )
30
+ add_files (" exercises/06_loop/main.cpp" )
31
+
32
+ target (" exercise07" )
33
+ set_kind (" binary" )
34
+ add_files (" exercises/07_struct/main.cpp" )
35
+
36
+ target (" exercise08" )
37
+ set_kind (" binary" )
38
+ add_files (" exercises/08_class/main.cpp" )
You can’t perform that action at this time.
0 commit comments