1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
levacccp [35]
3 years ago
13

The Fibonacci sequence {fib}i≥1 is defined recursively as follows: fib(1) = 1, fib(2) = 1 and, fib(n) = fib(n-1) + fib(n-2) for

n ≥ 3. The numbers in the Fibonacci sequence are called the Fibonacci numbers, for example: {1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, …} Implement an efficient function in Java that returns the nth (n ≤ 90) Fibonacci number. Example: fib(9) = 34. long fib(int n)
Computers and Technology
1 answer:
nadya68 [22]3 years ago
4 0
<h2>Answer:</h2>

// Create a class header definition

public class Fibonacci {

   

   //Create a method fib to return the nth term of the fibonacci series

   public static long fib(int n){

       

       //when n = 1, the fibonacci number is 1

       //hence return 1

       if (n <= 1){

           return 1;

       }

       

       //when n = 2, the fibonacci number is 1

       //hence return 2

       else if(n == 2){

           return 1;

       }

       

       //at other terms, fibonacci number is the sum of the previous two

       //numbers

       //hence, return the sum of the fib on the previous two numbers - n-1

       // and n-2

       else {

           return fib(n-1) + fib(n-2);

       }

       

   }

   

   //Create the main method to test the fib() method

   public static void main(String[] args) {

     

       // Test the fib method with n = 9        

       System.out.println(fib(9));

     

   }

}

=====================================================

<h2>Sample Output:</h2><h2></h2>

34

======================================================

<h2>Explanation:</h2>

The program above has been written in Java and it contains comments explaining each line of the code. Kindly go through the comments.

The actual lines of code are written in bold face to distinguish them from comments.

Also, a sample output of a run of the program has been provided.

You might be interested in
Which component, located on the motherboard, carries out all the instructions provided by computer programs? ram?
Blababa [14]
<span>The Chipset is the component located on the motherboard, this carries out all the instructions provided by computer programs. It is the integrated circuit that manages all the data flow between processor and memory. It usually works with microprocessors and plays a crucial role in determining system performance.</span>
6 0
3 years ago
_is a computer network created for an individual person
hichkok12 [17]

Answer:

Personal area network

Explanation:

is correct 100% sure

3 0
4 years ago
Amaya is curious how her computer actually boots up. Which of the
AleksAgata [21]
She should press OROM
6 0
3 years ago
Read 2 more answers
How to calculate 100MB at 56 Kbps to transfer time?
Gelneren [198K]
1MB=1000 kb 

\frac{100*1000Kb}{56Kb/s} ≈ 1748,71 seconds 


ransform in kilobyte and share knowing transfer time :)
3 0
3 years ago
WILL GIVE BRAINLIEST!!! PLEASE HELP!!!
padilas [110]

Answer:

BOI I got ricked rolled!

Explanation:

He done it!

5 0
3 years ago
Other questions:
  • What instruments are used the measure voltage, current, and resistance
    7·1 answer
  • Microsoft access does not create n:m relationships because microsoft access creates databases based on
    7·1 answer
  • Can somebody help me with this problem
    10·1 answer
  • In statistics, what is the mode of a data set?
    6·2 answers
  • Do you think an employer can accurately judge an applicant’s skills and character by reviewing his/her job application? Why or w
    14·1 answer
  • What function should be entered into B7 to calculate the total budget
    15·2 answers
  • Slide layouts can be changed by _________
    6·1 answer
  • PLEASE HELP!! I just joined this school and it was a online day but I didn’t have time to study because I’m new please help me i
    5·1 answer
  • what stage is the most inner part of the web architecture where data such as, customer names, address, account numbers, and cred
    11·1 answer
  • My Macbook computer charger is burned from the side od the cord. -_- Great way to start school. Anything i should do?
    15·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!