Question
PLEASE HELP COMPILE AND PLEASE PROVIDE SCREENSHOT. THANKS class fibonacci { // Recursive function to find the nth fibonacci number static long fib(int n) {
PLEASE HELP COMPILE AND PLEASE PROVIDE SCREENSHOT. THANKS
class fibonacci { // Recursive function to find the nth fibonacci number static long fib(int n) { // Base Case if (n <= 1) return n; // Recursive call return fib(n - 1) + fib(n - 2); } public static void main(String args[]) { int arr[]; arr = new int[3]; arr[0]=5; arr[1]=8; arr[2]=10; //arr contains value of n given in question i.e. 10,50,70 for (int i = 0; i < 1; i++) { System.out.print(fib(arr[i]) + " "); } } } Time Complexity : O(2^N)
Space Complexity : O(N)
And this is the dynamic programming approach.
class fibonacci { // dynamic programming method to find the nth fibonacci number. static long fib(int n) { long dp[]= new long[n+1]; dp[0]=0; dp[1]=1; for(int i = 2; i <= n; i++){ dp[i]=dp[i-1] + dp[i-2]; } return dp[n]; } public static void main(String args[]) { int arr[]; arr = new int[3]; arr[0]=10; arr[1]=50; arr[2]=70; //arr contains value of n given in question i.e. 10,50,70 for (int i = 0; i < 3; i++) { System.out.print(fib(arr[i]) + " "); } } }
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