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
The method __________ sets the font (helvetica, 20-point bold) in component
Stolb23 [73]
The method d) c.setFont(new Font("Helvetica", Font.BOLD, 20)) sets the font (Helvetica, 20­point bold) in component C. A set of instructions that can be called for execution using the method name is a method in Java that can take in data or parameters and return a value - both parameters and return values are optional.
7 0
3 years ago
Which describes the first step a crawler-based search engine uses to find information?
elena-14-01-66 [18.8K]
C is the correct answer to the problem
6 0
3 years ago
Read 2 more answers
What do you believe is the future of certification?
Gemiola [76]
Technology,performance,learning
7 0
3 years ago
Discuss the what is software development​
aleksandr82 [10.1K]

Answer:

software development is the conceiveing, specifying , designing, programing , texting, document and fix involved in creating and application frame work or other software components.

5 0
3 years ago
In order to practice as a lawyer specializing in digital media (or as a lawyer of any kind), you need to pass the state exam.
larisa [96]
The uniform bar exam
4 0
3 years ago
Other questions:
  • Develop an EER model for the following situation using the traditional EER notation, the Visio notation, or the subtypes inside
    8·1 answer
  • Write a character literal representing the (upper case) letter<br> a.
    13·1 answer
  • When you compile your program, the compiler identifies the logic errors and suggests how to correct them?
    14·1 answer
  • Compare and contrast the various options that exist for acquiring software.
    12·1 answer
  • Presentation software allows users to _____.
    15·1 answer
  • Use knowledge of the actions and adverse effects of NSAIDs to choose the correct statement.
    13·1 answer
  • Which of the following situations is least likely fair use
    6·2 answers
  • how do i create a program in little man computer that takes 2 numbers, divides them and then outputs the div and mod?
    8·1 answer
  • Draw the 2-3 tree that results when you insert the keys E A S Y Q U T I O N in that order into an initially empty tree
    12·1 answer
  • Convert 105 decimal to 128 binary form
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!