Question

Give pseudocode for a memoized algorithm that computes n factorial. You will want to think about...

Give pseudocode for a memoized algorithm that computes n factorial. You will want to think about the implementation of an appropriate data structure as well as a sentinel value for this problem.

Note: a memoized factorial algorithm is not considered dynamic programming, as factorial does not encounter repeated subproblems while recursing. However, memoizing this algorithm is the same process as memoizing a dynamic programming algorithm.

Homework Answers

Answer #1

Pseudo - code :

memory ={}

fact(n):
    if n in memory:
        return memory[n]
    elif n == 0:
        return 1
    else:
        x = fact(n-1) * n
        memory[n] = x
        return x

a = fact(10)
b = fact(20)

print a,b

What we are doing here, is, we created a list named memory, in which its ith index store factorial of i. This will fulfill the desire of memoized algorithm of computing factorial.

To compute factorial we are using recursion here.

//x = fact(n-1)*n

above is the recursion step.

At the end function fact(n) returns factorial of n.

Like, if this helped :)

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
You're are working on n different projects, but in m hours you are going on vacation....
You're are working on n different projects, but in m hours you are going on vacation. Imagine that for each project i, you had a function fi that told you how happy the people paying for project i would be if out your m available hours you devoted 0 ≤ x ≤ m to project i. Imagine further that each such function has maximum value at most s, corresponding to the project being fully finished (and thus the clients being...
T-tests are used when you want to examine differences but you do not know everything about...
T-tests are used when you want to examine differences but you do not know everything about the population. There are three types of t-tests that you may choose to do: one-sample t-test, independent sample t-test, or dependent sample t-test. You can calculate these by hand, in SPSS, or in Excel. The instructions below can be used for SPSS and your textbook offers instructions for using Excel. Single-sample t-tests These tests are used when you want to determine the probability that...
This is C programming assignment. The objective of this homework is to give you practice using...
This is C programming assignment. The objective of this homework is to give you practice using make files to compose an executable file from a set of source files and adding additional functions to an existing set of code. This assignment will give you an appreciation for the ease with which well designed software can be extended. For this assignment, you will use both the static and dynamic assignment versions of the matrix software. Using each version, do the following:...
Part A. Input Validation (Name your C program yourLastName_yourFirstName_Lab4a.c) 1. Place the code you developed in...
Part A. Input Validation (Name your C program yourLastName_yourFirstName_Lab4a.c) 1. Place the code you developed in Lab 2 to obtain a diameter value from the user and compute the volume of a sphere (we assumed that to be the shape of a balloon) in a new program, and implement the following restriction on the user’s input: the user should enter a value for the diameter which is at least 8 inches but not larger than 60 inches. Using an if-else...
The assignment summary sheets will be submitted week 14 with Test 3. You are to personally...
The assignment summary sheets will be submitted week 14 with Test 3. You are to personally experience the power and satisfaction of developing these skills firsthand and to reflect and write about this experience. Over the years, many students have shared amazingly rewarding experiences as they worked on these skills. The assignment will be evaluated and be weighted as 5% of your final mark (together, they are worth 20% of your mark for Test 3, which is worth 25% of...
About John Daniels Chemicals Inc. This business case is about John Daniels Chemicals Inc., which is...
About John Daniels Chemicals Inc. This business case is about John Daniels Chemicals Inc., which is one the most respected and elite chemical research organization in the industry, operating since 1991, with the headquarters in Tanzania, Africa. Organizational Structure and Culture at John Daniels Chemicals Inc. Organizational culture in John Daniels Chemicals Inc. is an open and less rigid one, unlike the other usual corporations in the market. The scientists selected to work in John Daniels Chemicals Inc. are top...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
Please read the article and answear about questions. Determining the Value of the Business After you...
Please read the article and answear about questions. Determining the Value of the Business After you have completed a thorough and exacting investigation, you need to analyze all the infor- mation you have gathered. This is the time to consult with your business, financial, and legal advis- ers to arrive at an estimate of the value of the business. Outside advisers are impartial and are more likely to see the bad things about the business than are you. You should...
What are 4 key things you learned about the topic from reading their paper? How does...
What are 4 key things you learned about the topic from reading their paper? How does the topic relate to you and your current or past job? Critique the paper in terms of the organization and quality. Team 3 answer questions above. Part I In today’s world we see fear among people when dealing with sexual harassment. This leads to people not reporting sexual harassment. A misconception about sexual harassment is that it’s only about touching and forcing other people...
Read the following case carefully and then answer the questions. In the movie Face/Off, John Travolta...
Read the following case carefully and then answer the questions. In the movie Face/Off, John Travolta got a new look by exchanging faces with Nicolas Cage. Unfortunately, he got a lot of trouble along with it. John could receive a much less troublesome new look by using Botox, a treatment discovered by Vancouver’s Dr. Jean Carruthers, who came upon the cosmetic potential of Botox in 1982 while treating a woman with eye spasms. Botox is marketed by Allergan, a specialty...