-
Notifications
You must be signed in to change notification settings - Fork 478
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: YdrMaster <[email protected]>
- Loading branch information
Showing
6 changed files
with
78 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#include <iostream> | ||
|
||
unsigned long long fibonacci(int i) { | ||
static unsigned long long cache[128]{0, 1}, next = 2; | ||
for (; next <= i; ++next) { | ||
cache[next] = cache[next - 1] + cache[next - 2]; | ||
} | ||
return cache[i]; | ||
} | ||
|
||
int main(int argc, char **argv) { | ||
auto fib100 = fibonacci(100); | ||
std::cout << "fibonacci(100) = " << fib100 << std::endl; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#include <iostream> | ||
|
||
struct Fibonacci { | ||
size_t cache[128]; | ||
int next = 2; | ||
|
||
Fibonacci() : cache{0, 1}, next(2) {} | ||
~Fibonacci() { | ||
std::cout << "Drop Fibonacci with next = " << next << std::endl; | ||
} | ||
|
||
size_t operator[](int i) { | ||
for (; next <= i; ++next) { | ||
cache[next] = cache[next - 1] + cache[next - 2]; | ||
} | ||
return cache[i]; | ||
} | ||
}; | ||
|
||
int main(int argc, char **argv) { | ||
Fibonacci fib0, fib1; | ||
std::cout << "fibonacci(10) = " << fib0[10] << std::endl; | ||
std::cout << "fibonacci(100) = " << fib1[100] << std::endl; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#include <iostream> | ||
|
||
class Fibonacci { | ||
size_t cache[128]; | ||
int next = 2; | ||
|
||
public: | ||
Fibonacci() : cache{0, 1}, next(2) {} | ||
~Fibonacci() { | ||
std::cout << "Drop Fibonacci with next = " << next << std::endl; | ||
} | ||
|
||
size_t operator[](int i) { | ||
for (; next <= i; ++next) { | ||
cache[next] = cache[next - 1] + cache[next - 2]; | ||
} | ||
return cache[i]; | ||
} | ||
}; | ||
|
||
int main(int argc, char **argv) { | ||
Fibonacci fib0, fib1; | ||
std::cout << "fibonacci(10) = " << fib0[10] << std::endl; | ||
std::cout << "fibonacci(100) = " << fib1[100] << std::endl; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters