Answered step by step
Verified Expert Solution
Link Copied!

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

image text in transcribed

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

DATABASE Administrator Make A Difference

Authors: Mohciine Elmourabit

1st Edition

B0CGM7XG75, 978-1722657802

More Books

Students also viewed these Databases questions

Question

What should you do if your firm operates in more than one industry?

Answered: 1 week ago

Question

3. What may be the goal of the team?

Answered: 1 week ago

Question

Is how things are said consistent with what is said?

Answered: 1 week ago