-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDriver.java
More file actions
49 lines (39 loc) · 1.44 KB
/
Copy pathDriver.java
File metadata and controls
49 lines (39 loc) · 1.44 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
import java.io.*;
import java.util.*;
class Driver {
public static void main(String[] args)
{
Random randomGenerator = new Random();
double sum;
double avg;
for(int i=12; i<=18; i++)
{
sum = 0;
for(int j=0; j<=10; j++)
{
long lowerBound = (long)Math.pow(10,i-1);
long upperBound = (long)Math.pow(10,i-1) + (long)Math.pow(10,i-1)-1;
long testNumber = lowerBound + (long)(randomGenerator.nextDouble()*(upperBound-lowerBound));
int counter = 0;
//Start clock
long startTime = System.nanoTime();
//Start calculation
for(int k = 2; k <= Math.sqrt(testNumber); k++)
{
if(testNumber%k == 0)
counter += 2;
}
//Stop clock
long endTime = System.nanoTime();
//Calculate time elapsed
long timeElapsedInMilliseconds = (endTime - startTime) / 1000000;
//Print results
System.out.println("The number of factors of the number " + testNumber + " is " + counter + ".");
System.out.println("That calculation took " + timeElapsedInMilliseconds + " milliseconds.");
sum += timeElapsedInMilliseconds;
}
avg = sum/10;
System.out.println("The average time taken for a " + i + " digit number is " + avg + " ms.");
}
}
}