Question

Looking at the recursive formula we can design a simple program use pseudocode. Fibonacci(n): if (n...

Looking at the recursive formula we can design a simple program use pseudocode. Fibonacci(n): if (n == 0) return 0; if (n == 1) return 1; return Fibonacci (n-1) + Fibonacci(n-2).

Discuss any issues you find with your program and what reasoning there may be.

Homework Answers

Answer #1

The psedudo code is present in the code blocks below. This is an iterative approach to solve this. Similarly the formula can be directly mapped to a recursive solution as well.

getFibonacci(int n) : method  {

define F: array to store fibonacci.
// initialise starting values.
F[0] = 0, F[1] = 1;

n: int storing the max fibonacci number to get.
iterate i from 2 to n:
  F[i] = F[i-1] + F[i-2];


return F[i];
}

The above code is for a method that returns the nth fibonacci number. The only place where this could create a problem is when we have to redo the same process of calculating the fibonacci number again. To mitigate that issue, we could store fibonacci numbers to its max value and then can just refere it next time instead of calculating it.

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
There is a Java program that is missing one recursive function: public class Fibonacci { /*...
There is a Java program that is missing one recursive function: public class Fibonacci { /* / 0 when n = 0 * fib(n) = | 1 when n = 1 * \ fib(n-1)+fib(n-2) otherwise */ public static int fib(int n) { return 0; } /* Fibonacci Test Framework * * Note, this takes a long time to compute fib(44). */ public static void main(String[] args) { int[] input = { 11, 22, 33, 44}; int[] expect = { 89,...
Recall the Fibonacci sequence: 1,1,2,3,8,13.... In general we can generate Fibonacci-like sequences by making linear combinations...
Recall the Fibonacci sequence: 1,1,2,3,8,13.... In general we can generate Fibonacci-like sequences by making linear combinations of the formula that gives us the nth term of the sequence.Consider the following general case for generating Fibonacci-like sequences: F 1 = 1 F 2 = 1 Fn =  aFn-1 + bFn-2 where a, b are integers . Write a function that given values a, b and n will print a Fibonacci-like sequence up to the nth term of the sequence. ( eg, if...
Design a pseudocode program that loads an array with the following 7 values. Add one more...
Design a pseudocode program that loads an array with the following 7 values. Add one more word (of your own choosing) for a total of 8 words. biffcomelyfezmottleperukebedraggledquisling Be sure to use lowercase, as shown above. This will make the processing easier. • Ask the user to enter a word • Search through this array until you find a match with the word the user entered. • Once you find a match, output "Yes, that word is in the dictionary"....
Design a recursive divide-and-conquer algorithm A(n) that takes an integer input n ≥ 0, and returns...
Design a recursive divide-and-conquer algorithm A(n) that takes an integer input n ≥ 0, and returns the total number of 1’s in n’s binary representation. Note that the input is n, not its binary representation. For example, A(9) should return 2 as 9’s binary representation is 1001, while A(7) should return 3 since 7 is 111 in binary. Note that your algorithm should have a running time of O(log n). Justify your answer. You need to do the following: (1)...
Written in MASM Assembly Problem Definition: Write a program to calculate Fibonacci numbers. • Display the...
Written in MASM Assembly Problem Definition: Write a program to calculate Fibonacci numbers. • Display the program title and programmer’s name. Then get the user’s name, and greet the user. • Prompt the user to enter the number of Fibonacci terms to be displayed. Advise the user to enter an integer in the range [1 .. 46]. • Get and validate the user input (n). • Calculate and display all of the Fibonacci numbers up to and including the nth...
Use the combinations formula to prove that in general C(n, r) = C(n, n - r)....
Use the combinations formula to prove that in general C(n, r) = C(n, n - r). Use the permutations formula to evaluate for any whole number n, P(n, 0). Explain the meaning of your result. Use the combinations formula and the definition of 0! to evaluate, for any whole number n, C(n, 0). Explain the meaning of your result. Suppose you have 35 songs for a playlist consisting of only 5 songs. How many different playlists can you have?
Programming Challenge B write a simple program that takes as input 2 16-bit strings of 0's...
Programming Challenge B write a simple program that takes as input 2 16-bit strings of 0's and 1's. (For example one string is assigned X and on string is assigned Y). Compute and and display the bitwise AND, bitwise OR, and bitwise XOR of the two string, (For example, the resultant string is Z). You may use the programming language of your choice. Alternatively, if you do not know a programming language, you may describe your algorithm in plain text...
Assignment 3 Chapter 2: Algorithm Discovery and Design More about Pseudocode Design an algorithm that is...
Assignment 3 Chapter 2: Algorithm Discovery and Design More about Pseudocode Design an algorithm that is given a positive integer N and determines whether N is a prime number, that is, not evenly divisible by any value other than 1 and itself. The output of your algorithm is either the message ‘not prime’, along with a factor of N, or the message ‘prime ‘ Many excellent simulations of sorting algorithms are available, examine them if they have questions about this...
question : Take the recursive Java program Levenshtein.java and convert it to the equivalent C program....
question : Take the recursive Java program Levenshtein.java and convert it to the equivalent C program. Tip: You have explicit permission to copy/paste the main method for this question, replacing instances of System.out.println with printf, where appropriate. You can get the assert function from assert.h. Try running the Java program on the CS macOS machines. You can compile and run the program following the instructions discussed in class. Assertions are disabled by default. You can enable assertions by running java...
# Description: This program is a function which calculates pi #per Leibniz formula,based on the number...
# Description: This program is a function which calculates pi #per Leibniz formula,based on the number of values passed to it. # Programmer: xxxxx def calculate_pi(n):     total, sign = 0, 1     for i in range(n):         term = 1 / (2 * i + 1)         total += term * sign         sign *= -1     total *= 4     return total n = int(input("How many terms: ")) print(calculate_pi(n)) How many terms: 12 3.058402765927333 HW In Puthon Modify...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT