Write a program called p1.py with the following functions: 1. A function named factorial_loop (num) that...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
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. 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.
Expert Answer:
Answer rating: 100% (QA)
Solution Below is an example of a Python program named p1py that fulfills the requirements you speci... View the full answer
Related Book For
Numerical Methods With Chemical Engineering Applications
ISBN: 9781107135116
1st Edition
Authors: Kevin D. Dorfman, Prodromos Daoutidis
Posted Date:
Students also viewed these programming questions
-
Planning is one of the most important management functions in any business. A front office managers first step in planning should involve determine the departments goals. Planning also includes...
-
You are required to write a Python program that will manage character (heroes and villain) information. Character (hero and villain) information will be stored in a text file that will be read in...
-
Describe the relationship of the AD, SRAS, and LRAS curves when the economy is in long-run macroeconomic equilibrium.
-
A man pushes on a piano with mass 180 kg so that it slides at constant velocity down a ramp that is inclined at 11.00 above the horizontal floor. Neglect any friction acting on the piano. Calculate...
-
Jack Falcinelli is filing his tax as a head of household. His taxable income is $133,896 and his W-2 form shows he has already paid $34,197.00. Calculate his tax refund or payment?
-
A snow-cone machine at an icecream shop costs \(\$ 15,000\). The machine is expected to generate profits of \(\$ 2,500\) each year of its 10-year useful life. At the end of the 10 years, the machine...
-
The following events and transactions relate to a single contribution. 1. A high-tech rm pledged to contribute $1 million in the company's common stock to a university's business school if the school...
-
(5) Strong Data Processing Inequality for the Divergence. Given finite alphabets X and Y, consider a conditional probability distribution W(y|x), for (x, y) Xxy, such that for some y. y, W (y, x) > c...
-
Explain the impact of accounting transactions in financial statements. Describe the elements and purpose of each financial statement. Discuss the components and use of financial analysis.
-
What types of decisions must be made by the issuer and its investment banker?
-
What are the market and product positioning opportunities offered by digital channels?
-
What two federal agencies enforce antitrust laws?
-
What actions should digital marketing managers take to safeguard consumer privacy and security?
-
What is capitation?
-
Bay Harbour Natural Skincare is a proud Canadian company that manufactures skincare products made of non-toxic, eco-friendly and cruelty-free materials. The mission of the company is to bring the...
-
QUESTION 9 HC-O-C-R R-C-O-CH HC-O-P-O-CH-CH-NH3* O || O a. Phosphatidic acid, Serine O b. Lysophosphatidic acid, Serine, Free FA O c. Lysophosphatidylserine, Free FA O d. 2 Free FAs, Serine, Glycerol...
-
Repeat the solution of Example 2.8 using GaussSeidel. Example 2.8 Solve the system by Jacobis method with an initial guess (0, 0). 2x1 + x = 2 x12x = -2 (2.13.7)
-
Consider nonlinear ordinary differential equation subject to initial conditions Convert this problem into a system of autonomous equations and initial conditions. y" + (xy)" + cos(x)y = y
-
Write a program to solve using implicit Euler. dy dt = y/2, y(0) = 2 (4.2.39)
-
Cumulative Normal distribution \(\Phi_{(\mu, \sigma)}\) and probability (a) \(X \sim \phi_{(0,1)}\); what is \(P(X \leq 1.43)\) ? (b) \(X \sim \phi_{(0,1)}\); what is \(P(X>1.43)\) ? (c) \(X \sim...
-
Inverse cumulative Normal distribution \(z\) (a) Find \(z_{0.05}\). (b) Find \(z_{0.95}\). (c) Let \(X \sim \phi_{(2,1)}\). Find \(a\) such that \(P(X \leq a)=0.05\). (d) Let \(X \sim \phi_{(2,1)}\)....
-
The Normal approximation (a) A discrete stochastic variable \(X\) has expected value \(\mu_{X}=3\) and \(\sigma_{X}=1.2\). Use the Normal approximation to find \(P(X \leq 4)\). (b) A continuous...
Study smarter with the SolutionInn App