Question

In either Java or C# Instead of a regular Fibonacci number, you are supposed to calculate...

In either Java or C#

Instead of a regular Fibonacci number, you are supposed to calculate a special one as follows:

F(n) = F(n-1)+ 2 * F(n-2) + 3 * F(n-3)

As an example, if F(0) = F(1) = F(2) = 1, then we have:

F(3) = F(2) + 2 * F(1) + 3 * F(0) = 1 + 2 + 3 = 6 F(4) = F(3) + 2 * F(2) + 3 * F(1) = 6 + 2 + 3 = 11

Given F(0), F(1), F(2), and N, your job is to calculate F(N).

Input Format

First number is F(0), second number is F(1), third number is F(2), and last number is N.

Example input: 1 1 1 4

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

import java.util.*;
class Solution{
   static int a,b,c,n;
   public static void main(String args[])
   {
       Scanner sc=new Scanner(System.in);
       a=sc.nextInt();
       b=sc.nextInt();
       c=sc.nextInt();
       n=sc.nextInt();
       sc.close();
       System.out.println(function(n));
   }
   static int function(int n1)
   { if(n1==0)
           return a;
       if(n1==1)
           return b;
       if(n1==2)
           return c;
      
       return function(n1-1)+2*function(n1-2)+3*function(n1-3);
         
   }
}

Tested Input(s):

a) 1 1 1 4 Output: 11

b)2 3 4 7 Output: 444  

The above code is written recursively such that the function calls itself every time.

The code is very simple.When even the called function parameter value becomes one of the 3 inputs a,b,c,  we return them.

else we return function(n1-1)+2*function(n1-2)+3*function(n1-3)

Add a comment
Know the answer?
Add Answer to:
In either Java or C# Instead of a regular Fibonacci number, you are supposed to calculate...
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
  • c++ fibonacci code using loops Here are 8 Fibonacci numbers: 1, 1, 2, 3, 5, 8,...

    c++ fibonacci code using loops Here are 8 Fibonacci numbers: 1, 1, 2, 3, 5, 8, 13, 21 Note that the first Fibonacci number is 1, F(1) = 1 The second Fibonacci number is 1, i.e. F(2) = 1 Other Fibonacci numbers in the sequence is the sum of two previous Fibonacci numbers. For example F(3) = F(2) + F(1). In general F(n) = F(n-1) + F(n-2) Write a program to do the following tasks. User entries are shown in...

  • Write an ARM assembly language subroutine (named nfibo) to calculate and return the n-th Fibonacci number....

    Write an ARM assembly language subroutine (named nfibo) to calculate and return the n-th Fibonacci number. Fibonacci numbers (or a Fibonacci sequence) are a series of numbers with a property that the next number in the series is a sum of previous two numbers. Starting the series from 0, 1 as the first two numbers we have 0, 1, (0 + 1) = 1, (1 + 1) = 2, (1 + 2) = 3, (2 + 3) = 5, (3...

  • use Java please. The Fibonacci Sequence Given the initial Fibonacci numbers 0 and 1, we can...

    use Java please. The Fibonacci Sequence Given the initial Fibonacci numbers 0 and 1, we can generate the next number by adding the two previous Fibonacci numbers together. For this sequence, you will be asked to take an input, denoting how many Fibonacci numbers you want to generate. Call this input upperFibLimit. The longest Fib sequence you should generate is 40 and the shortest you should generate is 1. So,1<upperFibLimit<40 The rule is simple given f(0) 0, f(1) 1 ....

  • Write a Java program that generates an array of Fibonacci numbers. Specifications: The program -Fills a...

    Write a Java program that generates an array of Fibonacci numbers. Specifications: The program -Fills a one-dimensional array with the first 30 Fibonacci numbers using a calculation to generate the numbers. Note: The first two Fibonacci numbers 0 and 1 should be generated explicitly as in -long[] series = new long[limit]; //create first 2 series elements series[0] = 0; series[1] = 1; -But, it is not permissible to fill the array explicitly with the Fibonacci series’ after the first two...

  • Please I need java code for this question with output: 4.19) The Fibonacci sequence is the...

    Please I need java code for this question with output: 4.19) The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8, .... Formally, it can be expressed as: f ib0 = 0 f ib1 = 1 f ibn = f ibn−1 + f ibn−2 Write a multithreaded program that generates the Fibonacci sequence using either the Java, Pthreads, or Win32 thread library. This program should work as follows: The user will enter on the command...

  • Using the C Language - Calculate and print the first 20 elements of the Fibonacci series....

    Using the C Language - Calculate and print the first 20 elements of the Fibonacci series. You must use the recursive function call method to calculate the elements. Output the elements of the series in a fixed column width table format and calculate Phi for each step. Output how many iterations it would take to approach the theoretical value of Phi within 0.1%. For example: N    Fibonacci Phi -------------------------------------------------- 1 0    N/A 2    1    N/A 3...

  • Using java programming. Question 1. Write a recursive function fibo(n) that returns the nth Fibonacci number...

    Using java programming. Question 1. Write a recursive function fibo(n) that returns the nth Fibonacci number which is defined as follows: fibo(0) = 0 fibo(1) = 1 fibo(n) = fibo(n-1) + fibo(n-2) for n >= 2 Question 2. Write a recursive function that calculates the sum of quintics: 1power of5 + 2power of5 + 3power of5 + … + n5 Question 3. Write a program to find a route from one given position to another given position for the knight...

  • Please help with this Fibonacci Code (C++)

    #include <iostream>void fibonacci (int n)// compute the Fibonacci number F sub n{   if (n <= 1)  cout << n << endl; // F sub 0 = 0 and F sub 1 = 1   else { // compute F sub n      int fn; int fnm2 = 0; int fnm1 = 1;      for (int i = 2; i<=n; i++)      {     fn = fnm1 + fnm2;     fnm2 = fnm1;     fnm1 = fn;      } // end of for      cout << fn << endl;   } // end of else} // end of fibonaccivoid main(){  int n;  cout << "Input n " << endl;  cin >> n;  cout << "The Fibonacci Number corresp to"  << n << " is ";  fibonacci(n);}

  • this is using MATLAB 2. Fibonacci sequence: A Fibonacci sequence is composed of elements created by...

    this is using MATLAB 2. Fibonacci sequence: A Fibonacci sequence is composed of elements created by adding the two previous elements. The simplest Fibonacci sequence starts with 1,1 and proceeds as follows: 1, 1, 2, 3, 5, 8, 13, . However, a Fibonacci sequence can be created with any two starting numbers. Create a MATLAB function called FL_fib_seq' (where F and L are your first and last initials) that creates a Fibonacci sequence. There should be three inputs. The first...

  • Linux and C Write a program that will calculate the nthelement of such an extended Fibonacci...

    Linux and C Write a program that will calculate the nthelement of such an extended Fibonacci sequence. For example, assuming that your executable program was called efib and that the shell prompt is "bash-4.4$ ", interaction with your program might be as follows: bash-4.4$./efib Enter a number: 0 F(0) is 0 bash-4.4$ /efib Enter a number: 1 F (1) is 1 bash-4.4$./efib Enter a number:1 P (-1) is 1 bash-4.4$ /efib Enter a number: 6 F(6) is 8 bash-4.4$/efib Enter...

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
Active Questions
ADVERTISEMENT