Last active
February 1, 2025 06:32
-
-
Save Odie/0ab6754677b1751947e205b351fc83a7 to your computer and use it in GitHub Desktop.
Short test program to test iron.nvim bracketed paste
This file contains hidden or 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
def factorial_iterative(n): | |
if n < 0: | |
return None # Factorial is not defined for negative numbers | |
# Base cases: 0! and 1! are both 1 | |
elif n == 0 or n == 1: | |
return 1 | |
else: | |
# Initialize the result to 1 | |
result = 1 | |
# Iterate from 2 to n (inclusive) | |
# Multiply the current result by i | |
for i in range(2, n + 1): | |
result *= i | |
return result | |
def factorial_recursive(n): | |
# Factorial is not defined for negative numbers | |
if n < 0: | |
return None | |
# Base cases: 0! and 1! are both 1 | |
elif n == 0 or n == 1: | |
return 1 | |
# Recursive case: n! = n * (n-1)! | |
else: | |
return n * factorial_recursive(n - 1) | |
print(f"The factorial_iterative of 5 is {factorial_iterative(5)}") | |
print(f"The factorial_recursive of 5 is {factorial_recursive(5)}") | |
raise ValueError("This is a test error to check line number reporting") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment