Skip to content

Commit e7ce71e

Browse files
Merge pull request #728 from swicaksono/master
Recursive algorithm
2 parents 6a3365c + 7e8753b commit e7ce71e

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

Recursive/gcd.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'''
2+
The GCD algorithm computes the greatest common divisor of two numbers A and B recursively.
3+
'''
4+
5+
def gcd(x, y):
6+
if (y==0): # base case
7+
return x
8+
else: # recursive case
9+
return gcd(y, x%y)
10+
11+
if __name__ == "__main__":
12+
print (gcd(x=100, y=20))

Recursive/n_choose_k.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'''
2+
The N choose K algorithm computes the binomial coefficient C(N, K)
3+
'''
4+
5+
def nchoosek(n, k):
6+
if (k == 0) or (k == n): # base case
7+
return 1
8+
else: # recursive case
9+
return nchoosek(n-1, k-1) + nchoosek(n-1, k)
10+
11+
if __name__ == "__main__":
12+
print (nchoosek(n=5, k=3))

0 commit comments

Comments
 (0)