Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Develop a Java program to implement the following algorithms using recursion and

ID: 3761513 • Letter: D

Question

Develop a Java program to implement the following algorithms using recursion and iteration and report the result and the elapsed time in nanoseconds for each version. Prompt the user for the value of parameters. Report the result of operation and the elapsed time in nanoseconds.

1. Summing series : 1/4 + 2/7 + 3/10 + …… + N/(3N+1)

2. Nth Fibonacci number

3. Towers of Hanoi (Recursive only): report the number of moves and description of each move (Disk number, FROM shaft , TO shaft) using a sequence of properly formatted text

Explanation / Answer

code for 3rd Program:

import java.util.Scanner;

public class towersofHanoi {

public void solve(int x, String fisrt, String auxiliary, String last) {

if (x == 1) {

System.out.println(x+" "+fisrt + " -> " + last);

} else {

solve(x - 1, fisrt, last, auxiliary);

System.out.println(x+" "+fisrt + " -> " + last);

solve(x - 1, auxiliary, fisrt, last);

}

}

public static void main(String[] args) {

towersofHanoi towersOfHanoi = new towersofHanoi();

System.out.print("Enter number of discs to the first shaft: ");

Scanner sc = new Scanner(System.in);

int discsToSahaft = sc.nextInt();

System.out.println("Disk number From -> To" );

long initTime = System.nanoTime();

towersOfHanoi.solve(discsToSahaft, "A", "B", "C");//shafts A, B, C

long elapsedTime = System.nanoTime() - initTime;

System.out.println("elapsed time:"+elapsedTime);

}

}


sample output:

code for 2nd program:

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

public class fiborecursive

{

public static void main(String[] args) throws IOException

{

System.out.print(" Enter the value of n : ");

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

int n = Integer.parseInt(br.readLine());

System.out.print(" Enter the value of x for non recusrion method : ");

BufferedReader br1=new BufferedReader(new InputStreamReader(System.in));

int x = Integer.parseInt(br1.readLine());

long initTime = System.nanoTime();

System.out.println(" "+n+"th value in fibonacci series using recursion: "+fibonacciRec(n));

long elapsedTime = System.nanoTime() - initTime;

System.out.println("elapsed time:"+elapsedTime);

long initTime1 = System.nanoTime();

System.out.println(" "+x+"th value in fibonacci series using non-recursion: "+fibonaccinonRec(x));

long elapsedTime1 = System.nanoTime() - initTime1;

System.out.println("elapsed time:"+elapsedTime1);

}

public static int fibonaccinonRec(int t){

int p=0;

int q=1;

int r;

if(t==1)

{

return p;

}

if(t==2)

{

return q;

}

for (int i=2;i<t;i++)

{

r=p+q;

p=q;

q=r;

}

return q;

}

public static int fibonacciRec(int t)

{

if(t==1)

{

return 0;

}

if(t==2)

{

return 1;

}

return fibonacciRec(t-1)+fibonacciRec(t-2);

}

}

Sample output:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote