Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Java Question The starter code is given as : import java.util.Scanner; public class RecursiveMultiply { public static void main(String[] args) { Scanner scan = new
Java Question
The starter code is given as :
import java.util.Scanner; public class RecursiveMultiply { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int x = scan.nextInt(); int y = scan.nextInt(); long start = System.currentTimeMillis(); long z = mult(x,y); long t = System.currentTimeMillis() - start; System.out.println(z); // Use the following if you want to find run time. //System.out.println("Elapsed time = " + t); } public static long mult(int a, int b) { return (long) a*b; } }
For the recursive method, I currently have:
if (a==1){
return b;}
if (a%2==1){
return (long) mult(a, b-1)+a;
}
else{
return (long) (mult(a, (b-1)/2)+a)+ (mult(a, (b-1)/2)+a);
}
I tested other codes and it failed the test for 20000000 20000000 becuase it wasnt efficient enough so I figured if I add a part for when a is negative and positive it would pass the efficiency test but I am having trouble implementing that.
Any help would be greatly appreciated.
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