-
Notifications
You must be signed in to change notification settings - Fork 2
[feature] Add factorial function which helps in calculating factorial… #65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -15,6 +15,14 @@ def is_even(number: int) -> bool: | |||||||||||||||||||||||||||||||||||||||
| """Check if a number is even.""" | ||||||||||||||||||||||||||||||||||||||||
| return number % 2 == 0 | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| def factorial(n: int) -> int: | ||||||||||||||||||||||||||||||||||||||||
| """Calculate the factorial of a number.""" | ||||||||||||||||||||||||||||||||||||||||
| if n == 0: | ||||||||||||||||||||||||||||||||||||||||
| return 1 | ||||||||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||||||||
| return n * factorial(n - 1) | ||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+19
to
+23
|
||||||||||||||||||||||||||||||||||||||||
| """Calculate the factorial of a number.""" | |
| if n == 0: | |
| return 1 | |
| else: | |
| return n * factorial(n - 1) | |
| """Calculate the factorial of a non-negative integer. | |
| Uses an iterative approach to avoid recursion limits. | |
| """ | |
| if n < 0: | |
| raise ValueError("factorial() not defined for negative values") | |
| result = 1 | |
| for i in range(2, n + 1): | |
| result *= i | |
| return result |
Copilot
AI
Feb 22, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unnecessary else statement after return. Since line 21 contains a return statement, the else on line 22 is redundant. The code on line 23 can be dedented to align with the if statement, which is more Pythonic and improves readability.
| else: | |
| return n * factorial(n - 1) | |
| return n * factorial(n - 1) |
Copilot
AI
Feb 22, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The factorial function does not handle negative numbers. For negative input values, the function will cause infinite recursion and eventually crash with a RecursionError. Consider adding input validation to handle negative numbers appropriately, either by raising a ValueError or returning a specific value.
Copilot
AI
Feb 22, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trailing whitespace detected. There are extra spaces at the end of this line, which violates PEP 8 style guidelines and can cause issues with version control systems.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing blank line before function definition. According to PEP 8 and the existing code style in this file (see lines before greet, calculate_sum, and is_even), there should be two blank lines before the function definition, but there is only one.