-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKaratsuba.java
178 lines (138 loc) · 6.47 KB
/
Karatsuba.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
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/// Java Program to Implement Karatsuba Algorithm
// Importing Random class from java.util package
import java.util.Random;
// Main class
class Karatsuba {
// Counter to count the number of primitive operations
public static int counter = 0;
// Main driver method
public static long mult(long x, long y) {
// Checking only if input is within range
// Increment counter for the comparison operations
counter += 3;
if (x < 10 && y < 10) {
// Multiplying the inputs entered
// Increment counter for the multiplication operation & return statement
counter += 2;
return x * y;
}
// Declaring variables in order to
// Find length of both integer
// numbers x and y
int noOneLength = numLength(x);
// Increment counter for the assignment and function calling operations
counter += 2;
int noTwoLength = numLength(y);
// Increment counter for the assignment and function calling operations
counter += 2;
// Finding maximum length from both numbers
// using math library max function
int maxNumLength = Math.max(noOneLength, noTwoLength);
// Increment counter for the assignment and function call operations
counter += 2;
// Rounding up the divided Max length
Integer halfMaxNumLength = (maxNumLength / 2) + (maxNumLength % 2);
// Increment counter for the assignment, division, addition and modulo
// operations
counter += 4;
// Multiplier
long maxNumLengthTen = (long) Math.pow(10, halfMaxNumLength);
// Increment counter for assignment and function call operations
counter += 2;
// Compute the expressions
long a = x / maxNumLengthTen;
long b = x % maxNumLengthTen;
long c = y / maxNumLengthTen;
long d = y % maxNumLengthTen;
// Increment counter for assignment, division and modulo operations
counter += 8;
// Compute all multiplying variables
// needed to get the multiplication
long z0 = mult(a, c);
long z1 = mult(a + b, c + d);
long z2 = mult(b, d);
// Increment counter for assignment and function call operations
counter += 8;
long ans = (z0 * (long) Math.pow(10, halfMaxNumLength * 2) +
((z1 - z0 - z2) * (long) Math.pow(10, halfMaxNumLength) + z2));
// Increment counter for the assignment, addition, subtraction and
// multiplication operations
counter += 10;
counter++; // Increment counter for the return statement
return ans;
}
// Method 1
// To calculate length of the number
public static int numLength(long n) {
int noLen = 0;
while (n > 0) {
noLen++;
n /= 10;
}
// Increment counter for the assignment, comparison, increment and division
// operations
counter += 5;
counter++; // Increment counter for the return statement
// Returning length of number n
return noLen;
}
// Method 2
// Main driver function
public static void main(String[] args) {
// Initialisation of variables
long expectedProduct;
// Increment counter for the initialisation of expectedProduct variable
counter++;
long actualProduct;
// Increment counter for the initialisation of actualProduct variable
counter++;
int numberOfDigits = 3;
// Increment counter for the initialisation of numberOfDigits variable
counter++;
Integer MAX_VALUE = 10000;
// Increment counter for the initialisation of MAX_VALUE variable
counter++;
// Boe creating an object of random class
// inside main() method
Random r = new Random();
counter++; // Increment counter for the initialisation of random variable
for (int i = 0; i < MAX_VALUE; i++) {
counter += 3; // Increment counter for the loop initialisation, condition and increment
long x = generateRandomNumber(numberOfDigits, r);
long y = generateRandomNumber(numberOfDigits, r);
counter += 4; // Increment counter for the assignment and function call operations
System.out.println("First Number: " + x + "\nSecond Number: " + y);
counter += 4; // Increment counter for the print statements
expectedProduct = x * y;
counter += 2; // Increment counter for assignment and multiplication operations
counter++; // Increment counter for the comparison operation
if (i == 9999) {
// Prove assertions catch the bad stuff.
expectedProduct = 1;
counter++; // Increment counter for assignment operation
}
actualProduct = mult(x, y);
counter += 2; // Increment counter for assignment and function call
// Again printing the expected and
// corresponding actual product
System.out.println("Expected: " + expectedProduct);
System.out.println("Actual: " + actualProduct);
counter += 4; // Increment counter for print statements
System.out.println("Total number of primitive operations = " + counter + "\n");
counter += 3; // Increment counter for the print statement
assert expectedProduct == actualProduct : "The expected result is not equal to the actual result";
counter++; // Increment counter for the assert statement
}
}
// Generate a random number with the specified number of digits
private static long generateRandomNumber(int numberOfDigits, Random random) {
long min = (long) Math.pow(10, numberOfDigits - 1);
counter += 3; // Increment the counter for the initialization of the min variable
long max = (long) Math.pow(10, numberOfDigits) - 1;
counter += 3; // Increment the counter for the initialization of the max variable
long randomNumber = random.nextLong(max - min + 1) + min;
counter += 4; // Increment the counter for the initialization of the randomNumber variable
counter++; // Increment the counter for the return statement
return randomNumber;
}
}