Question

Write this recursive function in Pascal and test it with small values of ’n’: F0 =...

Write this recursive function in Pascal and test it with small values of ’n’: F0 = F1 = F2 = 1

if n is even: Fn = nFn−1 + (n − 1)Fn−2 + (n − 2)Fn−3

if n is odd: Fn = (n − 2)Fn−1 + (n − 1)Fn−2 + nFn−3

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code

program recursion;

var
(* local variable declaration *)
n: integer;
result: integer;
  
(*function definition *)
function test (n: integer): integer;
var
(* local variable declaration *)
result: integer;

begin
if((n=0) or (n=1) or (n=2)) then
Exit(1);
  
if(n mod 2 = 0) then
result := n*test(n-1) + (n-1)*test(n-2) + (n-2)*test(n-3)
else
result := (n-2)*test(n-1) + (n-1)*test(n-2) + n*test(n-3);
  
Exit(result);
  
end;
  
begin
n := 6;
result := test(n);
writeln(result)
end.


Test Output

For n=6

For n=5

Add a comment
Know the answer?
Add Answer to:
Write this recursive function in Pascal and test it with small values of ’n’: F0 =...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The Fibonnaci sequence is a recursive sequence defined as: f0 = 1, f1 = 1, and...

    The Fibonnaci sequence is a recursive sequence defined as: f0 = 1, f1 = 1, and fn = fn−1 + fn−2 for n > 1 So the first few terms are 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, . . .. Write a function/procedure/algorithm that computes the sum of all even-valued Fibonnaci terms less than or equal to some positive integer k. For example the sum of all even-valued Fibonnaci terms less than or equal to 40...

  • Let f(x) be the recurrence relation defined by fn=fn-12+nfn-2 for n≥2 f0=3 f1=-1 Find f(3)

    Let f(x) be the recurrence relation defined by fn=fn-12+nfn-2 for n≥2 f0=3 f1=-1 Find f(3)

  • Let f0, f1, f2, . . . be the Fibonacci sequence defined as f0 = 0,...

    Let f0, f1, f2, . . . be the Fibonacci sequence defined as f0 = 0, f1 = 1, and for every k > 1, fk = fk-1 + fk-2. Use induction to prove that for every n ? 0, fn ? 2n-1 . Base case should start at f0 and f1. For the inductive case of fk+1 , you’ll need to use the inductive hypothesis for both k and k ? 1.

  • Please write code in C++ using recursive function Write a program that computes the sequence of...

    Please write code in C++ using recursive function Write a program that computes the sequence of Fibonacci numbers. The formula for generating the next Fibonacci number is: Fn = Fn−1 +Fn−2, where F1 = 1 and F2 = 2. For example, F3 = F2 + F1 = 2 + 1 = 3. You will notice that at some point Fibonacci numbers are too large and they do not fit in type int. This is called the integer overflow. When they...

  • F0 = 0, F1 = 1. Thus: F2 = F1 + F0 = 1 + 0...

    F0 = 0, F1 = 1. Thus: F2 = F1 + F0 = 1 + 0 = 1, F3 = F2 + F1 = 1 + 1 = 2, F4 = F3 + F2 = 2 + 1 = 3, ... Write a program that asks how many Fibonacci numbers to compute and then show each number. for C++ beginer

  • The Fibonacci numbers are defined as follows, f1=1, f2=1 and fn+2=fn+fn+1 whenever n>= 1. (a) Characterize...

    The Fibonacci numbers are defined as follows, f1=1, f2=1 and fn+2=fn+fn+1 whenever n>= 1. (a) Characterize the set of integers n for which fn is even and prove your answer using induction (b) Please do b as well. The Fibonacci numbers are defined as follows: fi -1, f21, and fn+2 nfn+1 whenever n 21. (a) Characterize the set of integers n for which fn is even and prove your answer using induction. (b) Use induction to prove that Σ. 1...

  • C++ Fibonacci Complete ComputeFibonacci() to return FN, where F0 is 0, F1 is 1, F2 is...

    C++ Fibonacci Complete ComputeFibonacci() to return FN, where F0 is 0, F1 is 1, F2 is 1, F3 is 2, F4 is 3, and continuing: FN is FN-1 + FN-2. Hint: Base cases are N == 0 and N == 1. #include <iostream> using namespace std; int ComputeFibonacci(int N) { cout << "FIXME: Complete this function." << endl; cout << "Currently just returns 0." << endl; return 0; } int main() { int N = 4; // F_N, starts at...

  • Fibonacci num Fn are defined as follow. F0 is 1, F1 is 1, and Fi+2 =...

    Fibonacci num Fn are defined as follow. F0 is 1, F1 is 1, and Fi+2 = Fi + Fi+1, where i = 0, 1, 2, . . . . In other words, each number is the sum of the previous two numbers. Write a recursive function definition in C++ that has one parameter n of type int and that returns the n-th Fibonacci number. You can call this function inside the main function to print the Fibonacci numbers. Sample Input...

  • Write a recursive function that accumulates the sum of values in an n element array and...

    Write a recursive function that accumulates the sum of values in an n element array and prints out the contents of the array and the final sum and average. In c language

  • ****python**** Q1. Write a recursive function that returns the sum of all numbers up to and...

    ****python**** Q1. Write a recursive function that returns the sum of all numbers up to and including the given value. This function has to be recursive; you may not use loops! For example: Test Result print('%d : %d' % (5, sum_up_to(5))) 5 : 15 Q2, Write a recursive function that counts the number of odd integers in a given list. This function has to be recursive; you may not use loops! For example: Test Result print('%s : %d' % ([2,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT