Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Example program
- #include <iostream>
- #include <string>
- using namespace std;
- int getFibN(int n);
- int getFibNSum(int n);
- int TEST();
- int main(){
- // TESTS
- int testResult = TEST(); if (testResult) return testResult;
- int n;
- cout << "n=";
- cin >> n;
- cout << "Summ fib(0) -> fib(n) = " << getFibNSum(n);
- }
- int getFibNSum(int n){
- int sum = 0;
- do {
- sum += getFibN(n);
- } while (n --> 1);
- return sum;
- }
- int getFibN(int n){
- int a = 0;
- int b = 1;
- while (n --> 0){
- int t = b;
- b = a+b;
- a = t;
- }
- return a;
- }
- int TEST(){
- if (getFibN(0) != 0) return 1;
- if (getFibN(1) != 1) return 2;
- if (getFibN(2) != 1) return 3;
- if (getFibN(3) != 2) return 4;
- if (getFibN(4) != 3) return 5;
- if (getFibN(5) != 5) return 6;
- if (getFibN(6) != 8) return 7;
- if (getFibN(7) != 13) return 8;
- if (getFibNSum(0) != 0) return 9;
- if (getFibNSum(1) != 1) return 10;
- if (getFibNSum(2) != 2) return 11;
- if (getFibNSum(3) != 4) return 12;
- if (getFibNSum(4) != 7) return 13;
- if (getFibNSum(5) != 12) return 14;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement