Answered step by step
Verified Expert Solution
Question
1 Approved Answer
1 Fibonacci Sequence The Fibonacci sequence is named after the Italian mathematician Leonardo of Pisa, also known as Fibonacci. The steps for the for the
1 Fibonacci Sequence The Fibonacci sequence is named after the Italian mathematician Leonardo of Pisa, also known as Fibonacci. The steps for the for the Fibonacci sequence are to start with the 0th and 1st terms, which are 0 and 1 respectively, and to calculate the next term, sum the previous two terms. Using the initial numbers 0+1=1 the 2nd term in the Fibonacci sequence is 1 . Now that you know the pattern, here are the 0th through 8th numbers in the Fibonacci sequence: 0,1,1,2,3,5,8,13,21. Now let's write a Fib class to help us find the nth Fibonacci number. Write the following functions in the Fib class: - public static int fibonacciRecursive(int n ) - this will return the nth Fibonacci number as an int using a recursive approach. - public static int fibonaccilterative(int n ) - this will return the nth Fibonacci number as an int using an iterative approach. Here are some test cases: Example Test Cases: - fibonacciRecursive(3) will return 2 - fibonacciRecursive(5) will return 5 - fibonacciIterative(8) will return 21 - fibonacciIterative(10) will return 55 Once you have your methods working, use the Scanner class to prompt for input in the main function of Fib. Hint: For a reminder on how to use Scanner class, the Scan.java file on Canvas is a good place to start. This example explains how to take ints as input. For example, to take in an integer for the Fibonacci functions, your Java code could be the following (in main): public static void main(String args []) \{ System.out.println("Enter an integer n to get the n 'th Fibonacci number:"); Scanner myScanner = new Scanner(System.in); int n= myScanner.nextInt ();// gets an integer from command line System.out.println("The " +n+ "' th Fibonacci number using fibonacciRecursive is " + fibonacciRecursive (n) ); System.out.println("The " +n+ "'th Fibonacci number using fibonacciIterative is " + fibonacciIterative (n) ); Reflection: When you run your method with large values like 80 or 200 what happens? Are there redundant calculations that are slowing down your method? How might you speed up your method by using an array, and how do your recursive and iterative approaches compare? (This is not a coding question, but rather something to think about) Milestone 1: Show a TA your methods and have them input numbers into your Scanner prompt to show correctness and then answer the following question: How does your Fibonacci method handle a negative value for n
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started