Using Python: The math module in Pyhon has a factorial function that does just w
ID: 3675780 • Letter: U
Question
Using Python:
The math module in Pyhon has a factorial function that does just what you did in 3 above. Which version of the factorial will take longer? To answer this question we’ll use the module timeit in Python. This module allows us to time small bits of Python code. The example below shows how to use this module. Now write a program that will time the two versions of factorial for the same value of n and answer the following questions:
a. How long it version takes to compute the following values: 100, 5000,10000,100000?
b. Which version is faster? By how much?
c. By trial end error what series can you compute with your version in 100 milliseconds, 1 second, and 5 seconds?
import timeit
tic = timeit.default_timer( )
# Code to measure time for goes here
toc = timeit.default_timer( )
elapsed_time = toc - tic
# this will be in seconds
Explanation / Answer
Please find the answers below :
a) Using math.factorial :
for n=100 : 9.98973846436e-05
for n=5000 : 0.0205299854279
for n=10000 : 0.0705988407135
for n=100000 : 7.29052209854
b) Using loop to find factorial of 100000 tooks : 7.60252213478 sec; which means math.factorial function is 4% faster than the other
c)
for 100 milliseconds we can compute around n = 1100
for 1 econd we can compute around n = 40000
for 5 econd we can compute around n = 86000
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.