1 | package de.uka.ipd.sdq.measurement.strategies.activeresource.cpu; |
2 | |
3 | import de.uka.ipd.sdq.measurement.strategies.activeresource.AbstractDemandStrategy; |
4 | import de.uka.ipd.sdq.measurement.strategies.activeresource.ResourceTypeEnum; |
5 | |
6 | public class CalculatePrimesDemand extends AbstractDemandStrategy { |
7 | |
8 | /** number to start the search with */ |
9 | private final long number = 2; |
10 | |
11 | /** |
12 | * Initialize the search for the next given number of primes |
13 | */ |
14 | public CalculatePrimesDemand() { |
15 | super(-3, 0, 3, 100, 100); |
16 | } |
17 | |
18 | /** |
19 | * Search for the next primes. |
20 | */ |
21 | private long calculatePrime(double numberNextPrimes) { |
22 | |
23 | boolean isPrime = true; |
24 | long currentNumber = number; |
25 | long primesFound = 0; |
26 | long currentDivisor; |
27 | long upperBound; |
28 | |
29 | while (primesFound < numberNextPrimes) { |
30 | // test primality of currentNumber |
31 | currentDivisor = 2; |
32 | upperBound = currentNumber / 2; |
33 | while ((currentDivisor < upperBound) && (isPrime)) { |
34 | isPrime = currentNumber % currentDivisor != 0; |
35 | currentDivisor++; |
36 | } |
37 | // count primes and continue |
38 | if (isPrime) { |
39 | primesFound++; |
40 | } |
41 | // prepare for next iteration |
42 | isPrime = true; |
43 | currentNumber++; |
44 | } |
45 | return currentNumber; |
46 | } |
47 | |
48 | @Override |
49 | public void run(long initial) { |
50 | calculatePrime(initial); |
51 | } |
52 | |
53 | @Override |
54 | public ResourceTypeEnum getStrategysResource() { |
55 | return ResourceTypeEnum.CPU; |
56 | } |
57 | |
58 | @Override |
59 | public String getName() { |
60 | return "CalculatePrimes"; |
61 | } |
62 | |
63 | @Override |
64 | public void cleanup() { |
65 | // Do nothing. |
66 | } |
67 | } |