-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogic.py
More file actions
34 lines (25 loc) · 1.13 KB
/
logic.py
File metadata and controls
34 lines (25 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Here is my function that calculates the factorial of a number.
# I wrote in a logical error, so every time it will return 0 instead of the correct answer.
# The range of the loop starts from 0 instead of 1.
# This is a logical error because the factorial of a number is defined as the product of all positive integers up to that number.
# Multiplication by 0 instead results in 0.
# The correct range should start from 1, not 0.
# Moreover, factorial multiplications include the number itself.
# range(end) will stop at end-1, so we need to account for this in the code.
def factorial(n):
fact = 1
# Logical error here. 0 should be excluded from the factorial, but range() starts at 0 by default.
# Logical error part 2: range(n) stops at n-1.
for i in range(n):
fact *= i
return fact
print(factorial(5))
# This is how it should be coded instead:
def fixed_factorial(n):
fact = 1
# Add a start argument to range(start, end) instead of only range(end) above:
# start=1, end=n+1 - this will give the correct integers.
for i in range(1, n + 1):
fact *= i
return fact
print(fixed_factorial(5))