-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
093d896
commit 8d795e5
Showing
4 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
""" | ||
Name:Joe George | ||
Title: Factorial of a number (With & Without Recursion) | ||
Date: 3 December 2024 | ||
""" | ||
|
||
# 1. WITHOUT RECURSION | ||
def factorial(n): | ||
result = 1 | ||
for i in range(1,n+1): | ||
result *= i | ||
return result | ||
|
||
print(factorial(4)) | ||
|
||
|
||
# 2. WITH RECURSION | ||
def recursion_factorial(n): | ||
if n == 1: | ||
return 1 | ||
else: | ||
return n*factorial(n-1) | ||
|
||
print(recursion_factorial(4)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
''' | ||
Name:Joe George | ||
Title: Greatest Common Divisor. | ||
Date: 3 December 2024 | ||
''' | ||
|
||
# Greatest Common Divisor | ||
def gcd(a,b): | ||
if a % b==0: | ||
return b | ||
else: | ||
return gcd(b,a%b) | ||
|
||
print(gcd(12,16)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
""" | ||
Name:Joe George | ||
Title: Addition using recursion | ||
Date: 3 December 2024 | ||
""" | ||
|
||
def add_nums(a, b): | ||
if b == 0: | ||
return a | ||
else: | ||
return add_nums(a+1, b-1) | ||
|
||
print(add_nums(10, 10)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
""" | ||
Name:Joe George | ||
Title: Multiplication using recursion | ||
Date: 3 December 2024 | ||
""" | ||
|
||
def multiply(a, b): | ||
if b == 1: | ||
return a | ||
else: | ||
return a + multiply(a, b-1) | ||
|
||
print(multiply(10, 10)) |