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...
How to measure the time complexity of an algorithm? Identify an important operation in the algorithm...
How to measure the time complexity of an algorithm? Identify an important operation in the algorithm that is executed most frequently. Express the number of times it is executed as a function of N. Convert this expression into the Big-O notation. A. For each of the three fragments of code, what is its worst-case time complexity, in the form "O(…)". (Use the given solution to the first problem as a model)                 //----------------- This is a sample problem – solved ------...
Suppose you are given a set S = {a1,a2,···,an} of tasks, where task ai requires pi...
Suppose you are given a set S = {a1,a2,···,an} of tasks, where task ai requires pi units of processing time to complete, once it has started. You have one computer on which to run these tasks, and the computer can run only one task at a time. Let ci be the completion time of task ai, that is, the time at which task ai completes processing. Your goal is to minimize the average completion time, that is, to minimize n1...
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:...
300 words:      (b) Do you think that it is appropriate for firms like Black Diamond...
300 words:      (b) Do you think that it is appropriate for firms like Black Diamond to scrutinize its partner factories like this?      (c) Why or why not? case study Task: Read the “Black Diamond Equipment” case below and then answer the following questions. >> The way that Black Diamond is run, I don't really consider this the American way, I consider Black Diamond an extension of the attitude, the culture, the ethos, and the values of the life...
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...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT