-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicFactorDriver.java
More file actions
60 lines (53 loc) · 1.78 KB
/
Copy pathDynamicFactorDriver.java
File metadata and controls
60 lines (53 loc) · 1.78 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
import java.util.*;
import java.lang.*;
//static scheduling
class DynamicFactorDriver extends Thread
{
static int numDigits = 18;
static long lowerBound = (long)Math.pow(10,numDigits-1);
static long upperBound = (long)Math.pow(10,numDigits-1) + (long)Math.pow(10,numDigits-1)-1;
static int numThreads=2;
static int counter=2;
static long testNumber;
static long sK=2;
long k;
public void run()
{
while (sK*sK<testNumber)
{
synchronized(this)
{
k=sK;
sK+=8;
}
for(long i=k; i<k+8; i++)
{
if(testNumber%i==0)
counter += 2;
}
}
}
public static void createTestNumber()
{
Random randomGenerator = new Random();
testNumber = lowerBound + (long)(randomGenerator.nextDouble()*(upperBound-lowerBound));
}
public static void main(String args[]) throws Exception
{
new DynamicFactorDriver().createTestNumber();
DynamicFactorDriver[] th =new DynamicFactorDriver[numThreads];
long t1=System.currentTimeMillis();
for(int i=0;i<numThreads;i++)
{
th[i]=new DynamicFactorDriver();
th[i].start();
}
for(int i=0;i<numThreads;i++)
{
th[i].join();
}
long t2=System.currentTimeMillis();
System.out.println("The number of factors of the number " + testNumber + " is " + counter + ".");
System.out.println("That calculation took " + (t2-t1) + " milliseconds.");
}
}