Question: Write a program called p1.py with the following functions: 1. A function named factorial_loop (num) that takes a positive integer num 2 1 and
Write a program called p1.py with the following functions: 1. A function named factorial_loop (num) that takes a positive integer num 2 1 and returns the value of num! = num (num-1) * (num-2) * * 2 * 1. This function must use a looping control structure to compute the value of'num!'. 2. Another function named factorial_recursive (num) that takes a positive integer num 21 and returns num! as defined above but this function must be recursive. Remember to define at least one base case (that returns a value immediately) and the required recursive cases (that call the same function using a smaller parameter value) for this function. ... 3. Add a main function that performs the empirical runtime test on the two factorial functions that you implemented in 1.1 and 1.2. Design the test as follows. a. Initialize total_nums to a value between 500 and 900. b. Create a list of total_nums random integers in the range [1, total_nums], inclusive. c. Use perf_counter () method from the time module to record the total time to run the factorial_loop() function for each value in total_nums. d. Repeat step (c) to record the time to run the factorial_recursive () function for each value in total_nums. e. Which one do you think has a better runtime? You can discuss the results with the TAs and your peers. Tips: you can modify the code for factorial_loop() and factorial_recursive() discussed in class.
Step by Step Solution
3.48 Rating (151 Votes )
There are 3 Steps involved in it
Solution Below is an example of a Python program named p1py that fulfills the requirements you speci... View full answer
Get step-by-step solutions from verified subject matter experts
