-
Notifications
You must be signed in to change notification settings - Fork 2
/
GCDTest.java
50 lines (39 loc) · 1.3 KB
/
GCDTest.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
/**
* Matt Adelman.
* GCD Test
*/
import java.util.Random;
import java.math.BigInteger;
import java.lang.Long;
public class GCDTest {
public static void main(String[] args) {
// For creating random primes
Random rand = new Random();
// Bit length to test
int bits = 1024;
// arrays of big ints
BigInteger[] num1 = new BigInteger[100];
BigInteger[] num2 = new BigInteger[num1.length];
// GCD array
BigInteger[] data = new BigInteger[num2.length];
BigInteger big;
// Uses the same big int for num1[i] and num2[i+1] therefore
// we only need to generate num1.length big ints
for (int i = 0; i < num1.length - 1; i++) {
big = BigInteger.probablePrime(bits, rand);
num1[i] = big;
num2[i + 1] = big;
}
// The last and first ones
big = BigInteger.probablePrime(bits, rand);
num1[num1.length - 1] = big;
num2[0] = big;
// Timing of gcd
Long startTime = new Long(System.currentTimeMillis());
for (int i = 0; i < num1.length; i++) {
data[i] = num1[i].gcd(num2[i]);
}
Long endTime = new Long(System.currentTimeMillis());
System.out.println(endTime.intValue() - startTime.intValue());
}
}