-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path01-recursion.cpp
More file actions
49 lines (38 loc) · 1.06 KB
/
01-recursion.cpp
File metadata and controls
49 lines (38 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
using namespace std;
int fibonacci_posposicion(int pos) {
if( pos == 0 ) return 0;
if( pos == 1 ) return 1;
return fibonacci_posposicion(pos - 1) + fibonacci_posposicion(pos - 2);
}
/*************************************/
int fibonacci_aux(int pos, int a, int b) {
if( pos == 0 ) return b;
return fibonacci_aux(pos-1, b, a+b);
}
int fibonacci_cola(int pos) {
if( pos == 0 ) return 0;
if( pos == 1 ) return 1;
return fibonacci_aux(pos-2, 1, 1);
}
/*************************************/
int factorial_posposicion(int n) {
if( n == 0 || n == 1 ) return 1;
return n * factorial_posposicion(n-1);
}
/*************************************/
int factorial_aux(int n, int f) {
if( n == 1) return f;
return factorial_aux(n-1, f*n);
}
int factorial_cola(int n) {
if( n == 0 || n == 1 ) return 1;
return factorial_aux(n-1, n);
}
/*************************************/
/*************************************/
int main() {
for(int i = 0; i <= 10; i++)
cout << factorial_cola(i) << "\n";
return 0;
}