-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathRecursiveMultiply.java
49 lines (35 loc) · 1.1 KB
/
RecursiveMultiply.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import java.util.Scanner;
public class RecursiveMultiply {
public static long result = 0L;
public static long x = 0L;
public static int count = 0;
public static void main(final String[] args) {
Scanner usrInput = new Scanner(System.in);
System.out.println("Enter first and second number (separated by whitespace):");
String stringNums = usrInput.nextLine();
String[] stringNumList = stringNums.split(" ");
int numOne = Integer.parseInt(stringNumList[0]);
int numTwo = Integer.parseInt(stringNumList[1]);
System.out.println(mult(numOne, numTwo));
//System.out.println(mult(20000000, 20000000));
}
public static long mult(int a, int b){
if(count == 0){
x = (long) a;
count++ ;
}
if( b <= 0 ){
b = 0;
} else if( b % 2 == 0 ){
x = x << 1 ;
b = b >> 1;
return mult(a, b);
} else{
RecursiveMultiply.result = RecursiveMultiply.result + x;
x = x << 1 ;
b = b >> 1 ;
return mult(a, b);
}
return RecursiveMultiply.result;
}
}