Question: A factorial (n) function is defined, in order to calculate and return the factorial of n. In order to avoid calculation every time the

A factorial (n) function is defined, in order to calculate and return


A factorial (n) function is defined, in order to calculate and return the factorial of n. In order to avoid calculation every time the function is called, factorial_values_dict cache is used, where the previous calculated factorial values are stored. Below codes are the implementation of the factorial function using recursion, but contains some errors. List and explain all the errors in the codes. def factorial (n): Calculate & return n!=n* (n-1)*..*1 factorial_values_dict = {} The cache * Get the value in cache, if it's already there if n in factorial_values_dict: f = factorial_values_dict[n] If not, calculate & save in the cache else: f=nfactorial (n-1) factorial values_dict [n-1] = f (4 markah/marks) Berikan versi kod yang betul, dengan masih menggunakan fungsi factorial (n) rekursif dan menggantikan semua kesilapan dengan kod-kod yang betul. Provide a correct version of the code, while still using the recursive factorial (n) function, by replacing all the errors with correct codes. (4 markah / marks) 3/12

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!