1# modified code tranclated to python from (Drab Duck)
2
3def fact(num):
4 if num <= 1:
5 return 1
6 else:
7 return num*fact(num-1)
8
1/*Java*/
2static void recursion(){ recursion(0); }
3static void recursion(int x){
4 System.out.println("eheh " + x);
5 if(x != 666) recursion(x+1);
6}
1Did you mean: recursion
2Did you mean: recursion
3Did you mean: recursion
4Did you mean: recursion
5Did you mean: recursion
6Did you mean: recursion
7Did you mean: recursion
8Did you mean: recursion
9Did you mean: recursion
10Did you mean: recursion
11Did you mean: recursion
12Did you mean: recursion
13Did you mean: recursion
14Did you mean: recursion
15Did you mean: recursion
16Did you mean: recursion
17Did you mean: recursion
18Did you mean: recursion
19Did you mean: recursion
20Did you mean: recursion
21Did you mean: recursion
22Did you mean: recursion
23Did you mean: recursion
24Did you mean: recursion
25Did you mean: recursion
26Did you mean: recursion
27Did you mean: recursion
28Did you mean: recursion
29Did you mean: recursion
30Did you mean: recursion
31Did you mean: recursion
32Did you mean: recursion
33Did you mean: recursion
34Did you mean: recursion
35Did you mean: recursion
36Did you mean: recursion
37Did you mean: recursion
38Did you mean: recursion
39Did you mean: recursion
40Did you mean: recursion
41Did you mean: recursion
42Did you mean: recursion
43Did you mean: recursion
44Did you mean: recursion
45Did you mean: recursion
46Did you mean: recursion
47Did you mean: recursion
48Did you mean: recursion
49Did you mean: recursion
50Did you mean: recursion
51Did you mean: recursion
52Did you mean: recursion
53Did you mean: recursion
54Did you mean: recursion
55Did you mean: recursion
56Did you mean: recursion
57Did you mean: recursion
58Did you mean: recursion
59Did you mean: recursion
60Did you mean: recursion
61Did you mean: recursion
62Did you mean: recursion
63Did you mean: recursion
64Did you mean: recursion
65Did you mean: recursion
66Did you mean: recursion
67Did you mean: recursion
68Did you mean: recursion
69Did you mean: recursion
70Did you mean: recursion
71Did you mean: recursion
72Did you mean: recursion
73Did you mean: recursion
74Did you mean: recursion
75Did you mean: recursion
76Did you mean: recursion
77Did you mean: recursion
78Did you mean: recursion
79Did you mean: recursion
80Did you mean: recursion
81Did you mean: recursion
82Did you mean: recursion
83Did you mean: recursion
84Did you mean: recursion
85Did you mean: recursion
86Did you mean: recursion
87Did you mean: recursion
88Did you mean: recursion
89Did you mean: recursion
90Did you mean: recursion
91Did you mean: recursion
92Did you mean: recursion
93Did you mean: recursion
94Did you mean: recursion
95Did you mean: recursion
96Did you mean: recursion
97Did you mean: recursion
98Did you mean: recursion
99Did you mean: recursion
100Did you mean: recursion
1int function(int value) {
2 if(value < 1)
3 return;
4 function(value - 1);
5
6 printf("%d ",value);
7}
1
2#https://github.com/aspittel/coding-cheat-sheets/blob/master/fundamentals/recursion.md
3
4def factorial(n):
5 if n <= 1:
6 return 1
7 return n * factorial(n - 1)
8
9# Memoization
10# Memoization is often useful in recursion. This is when the results of a function call are remembered so they can be used again in later function calls. This is like caching your results to use again.
11
12# Code Example
13
14factorial_memo = {}
15
16def factorial(n):
17 if n <= 1:
18 return 1
19 elif n not in factorial_memo:
20 factorial_memo[n] = n * factorial(n-1)
21 return factorial_memo[n]
22# Tail Recursion
23# Tail recursion is where calculations are performed first and then the recursive call is executed. Some programming languages, usually functional ones, optimize tail calls so they take up less room on the call stack.
24
25def factorial(n, running_total=1):
26 if n <= 1:
27 return running_total
28 return factorial(n-1, n * running_total)
1def is_divisible(x, y):
2 if x % y == 0:
3 return True
4 else:
5 return False
6
7def is_power(a, b):
8 if a == 1 or b == 1: # a more succinct way to test base case
9 return a == 1
10 return is_divisible(a, b) and is_power(a/b, b) # divisible and recursions report True?
11
1def recursive_function(parameters):
2 if base_case_condition(parameters):
3 return base_case_value
4 recursive_function(modified_parameters)