Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- long long factor(int n) {
- cout << "call " << n << endl;
- if (n <= 1)
- return 1;
- return n * factor(n - 1);
- }
- long long factor2(int n) {
- long long res = 1;
- while (n > 1)
- res *= n--;
- return res;
- }
- void show(int size, int arr[]) {
- if (size == 0)
- return;
- cout << arr[0] << " ";
- show(size - 1, arr+1);
- }
- void show2(int size, int arr[]) {
- if (size == 0)
- return;
- show(size - 1, arr);
- cout << arr[size - 1] << " ";
- }
- int get_pow(int x, int y) {
- if (y == 0)
- return 1;
- return x * get_pow(x, y - 1);
- }
- int fibo(int n) {
- if (n < 2)
- return 1;
- return fibo(n - 1) + fibo(n - 2);
- }
- int fibo2(int n) {
- if (n < 2)
- return 1;
- int f_pred = 1;
- int f_pred_pred = 1;
- int f = 1;
- for (int k = 1; k < n; k++) {
- f = f_pred + f_pred_pred;
- f_pred_pred = f_pred;
- f_pred = f;
- }
- return f;
- }
- void test_recursion() {
- cout << "5! = " << factor(5) << endl;
- cout << "5! = " << factor2(5) << endl;
- const int size = 5;
- int arr[size]{ 501, 502, 503, 504, 505 };
- show(size, arr);
- cout << endl;
- show2(size, arr);
- cout << endl;
- cout << get_pow(3, 4) << endl;
- //3*3*3*3 = 9 * 9 = 81
- for (int n = 1; n < 20; n++) {
- cout << (double)fibo(n) / fibo(n-1) << " ";
- }
- cout << endl;
- for (int n = 0; n < 20; n++) {
- cout << fibo2(n) << " ";
- }
- cout << endl;
- }
- int main() {
- test_recursion();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement