-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOperations.java
More file actions
76 lines (62 loc) · 1.73 KB
/
Operations.java
File metadata and controls
76 lines (62 loc) · 1.73 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package com.ub.viks;
import java.util.Timer;
public class Operations {
//perform addition,multiplication and division without operators
private int result;
private long time;
private void additionWithoutOperation(int a,int b) {
}
private int recMultTable(int noOfAddition,int no) {
if(noOfAddition == 1) {
result = no;
}else {
result = no + recMultTable(noOfAddition -1, no);
}
return result;
}
private int recMultiplication(int noOfAddition,int no) {
if (noOfAddition > 0) {
// if((noOfAddition % 2) == 1) {
// result += no;
// }
result = result + no + recMultiplication((noOfAddition/2), no);
return result;
}
return result + result;
}
private void performMultiplication(int noOfAddition,int no) {
//naive approach
int i = 0;
for(i=0;i<noOfAddition;i++){
result += no;
}
}
private void multWithoutSign(int first,int second) {
//add the numbers to simulate multiplication
int diff = first - second;
long time1 = System.currentTimeMillis();
if(diff <= 0) {
//use second operand to determine number of addition
// performMultiplication(first,second);
// recMultiplication(first, second);
recMultTable(first, second);
}else {
//use first operand to determine number of addition
// performMultiplication(second,first);
// recMultiplication(second, first);
recMultTable(second, first);
}
long time2 = System.currentTimeMillis();
time = time2-time1;
}
private void divWithoutSign() {
//
}
public static void main(String args[]) {
long time = System.currentTimeMillis();
Operations oper = new Operations();
// oper.multWithoutSign(100000,100000);
oper.multWithoutSign(10, 1000000);
System.out.println("Result :"+oper.result + "\n time :"+oper.time);
}
}