Somebody help! I need help answering the following questions about my code below
ID: 3806837 • Letter: S
Question
Somebody help! I need help answering the following questions about my code below.
If you use an n of 5, how many lines of code will be executed?
Create a binary tree that stores your calls.
How many calls will each internal node make?
How many internal nodes will you have for a recursive version of the solution?
Now, how big is that number?
What if you call 5 n and think of Fib(n). What is the runtime complexity of your solution?
Do you think you can do better if you simply do away with the recursion and calculate the Fibonacci series iteratively?
Here is my code
package assignment_4_1;
import java.util.Scanner;
import java.util.Stack;
class calc
{
long fibo(int n)
{
if(n==0)
return 0;
if(n==1)
return 1;
else
return fibo(n-1)+fibo(n-2);
}
}
public class Assignment_4_1
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
Stack stack = new Stack();
System.out.println("Enter fibonacci Term :");
int n=sc.nextInt();
System.out.println("Fibonacci serious in descending oprder is : ");
calc c=new calc();
for(int i=0;i<n;i++)
{
stack.push(c.fibo(i));
}
for(int i = 0;i<n;i++)
{
System.out.println(" "+stack.pop());
}
}
}
Explanation / Answer
import java.util.Scanner;
import java.util.Stack;
class calc
{
long fibo(int n)
{
if(n==0)
return 0;
if(n==1)
return 1;
else
return fibo(n-1)+fibo(n-2);
}
}
public class Assignment_4_1
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
Stack stack = new Stack();
System.out.println("Enter fibonacci Term :");
int n=sc.nextInt();
System.out.println("Fibonacci serious in descending oprder is : ");
calc c=new calc();
for(int i=0;i<n;i++)
{
stack.push(c.fibo(i));
}
for(int i = 0;i<n;i++)
{
System.out.println(" "+stack.pop());
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.