Question: def factorial ( n ) : result = 1 for i in range ( 1 , n + 1 ) : result * = i

def factorial(n):
result =1
for i in range(1, n +1):
result *= i
return resultprint("Factorial of 4:", factorial(4))
print("Factorial of 5:", factorial(5))
print("Factorial of 6:", factorial(6))def choose(n, k):
return factorial(n)//(factorial(k)* factorial(n - k))
print("Choose 5,2:", choose(5,2))
print("Choose 6,3:", choose(6,3))
print("Choose 10,5:", choose(10,5))def recursive_factorial(n):
if n ==0 or n ==1:
return 1
else:
return n * recursive_factorial(n -1)
print("Recursive factorial of 5:", recursive_factorial(5))
print("Recursive factorial of 6:", recursive_factorial(6))
Factorial of 4: 24For Homework 3, you had to make two different function to calculate factorials. One
used a for loop, and one used a recursive function that called itsself.
A) Which of these function was faster for calculating large factorials? (Say,100!) You'll
needed to use
import timeit
or something similar to time the two functions.
B) What is the largest factorial your functions can calculate as a floating point number,
before they return infinity? You might need to modify your factorial function to return a
float by setting the initial number (0!) to 1.0
def factorial ( n ) : result = 1 for i in range (

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!