@@ -10,7 +10,7 @@ public class ApproximateCounting {
10
10
* It returns the approximate count
11
11
*/
12
12
static double n (double v , double a ) {
13
- return a * (Math .pow (1 + 1 / a , v ) - 1 );
13
+ return a * (Math .pow (1 + 1 / a , v ) - 1 );
14
14
}
15
15
16
16
@@ -21,13 +21,13 @@ static double n(double v, double a) {
21
21
* It returns the new value for v
22
22
*/
23
23
static double increment (double v , double a ) {
24
- double delta = 1 / (n (v + 1 , a ) - n (v , a ));
24
+ double delta = 1 / (n (v + 1 , a ) - n (v , a ));
25
25
26
- if (Math .random () <= delta ) {
27
- return v + 1 ;
28
- } else {
29
- return v ;
30
- }
26
+ if (Math .random () <= delta ) {
27
+ return v + 1 ;
28
+ } else {
29
+ return v ;
30
+ }
31
31
}
32
32
33
33
@@ -39,13 +39,13 @@ static double increment(double v, double a) {
39
39
* It returns the new value for v
40
40
*/
41
41
static double approximateCount (int nItems , double a ) {
42
- double v = 0 ;
43
-
44
- for (int i = 1 ; i < nItems + 1 ; i ++) {
45
- v = increment (v , a );
46
- }
42
+ double v = 0 ;
43
+
44
+ for (int i = 1 ; i < nItems + 1 ; i ++) {
45
+ v = increment (v , a );
46
+ }
47
47
48
- return n (v , a );
48
+ return n (v , a );
49
49
}
50
50
51
51
/*
@@ -57,26 +57,26 @@ static double approximateCount(int nItems, double a) {
57
57
* It terminates the program on failure
58
58
*/
59
59
static void testApproximateCount (int nTrails , int nItems , double a , double threshold ) {
60
- double avg = DoubleStream .generate (() -> approximateCount (nItems , a ))
61
- .limit (nTrails )
62
- .average ()
63
- .getAsDouble ();
64
-
65
- if (Math .abs ((avg - nItems ) / nItems ) < threshold ) {
66
- System .out .println ("passed" );
67
- }
60
+ double avg = DoubleStream .generate (() -> approximateCount (nItems , a ))
61
+ .limit (nTrails )
62
+ .average ()
63
+ .getAsDouble ();
64
+
65
+ if (Math .abs ((avg - nItems ) / nItems ) < threshold ) {
66
+ System .out .println ("passed" );
67
+ }
68
68
}
69
69
70
70
71
71
public static void main (String args []) {
72
- System .out .println ("testing 1,000, a = 30, 1% error" );
73
- testApproximateCount (100 , 1_000 , 30 , 0.1 );
72
+ System .out .println ("testing 1,000, a = 30, 1% error" );
73
+ testApproximateCount (100 , 1_000 , 30 , 0.1 );
74
74
75
- System .out .println ("testing 12,345, a = 10, 1% error" );
76
- testApproximateCount (100 , 12_345 , 10 , 0.1 );
77
-
78
- System .out .println ("testing 222,222, a = 0.5, 10% error" );
79
- testApproximateCount (100 , 222_222 , 0.5 , 0.2 );
75
+ System .out .println ("testing 12,345, a = 10, 1% error" );
76
+ testApproximateCount (100 , 12_345 , 10 , 0.1 );
77
+
78
+ System .out .println ("testing 222,222, a = 0.5, 10% error" );
79
+ testApproximateCount (100 , 222_222 , 0.5 , 0.2 );
80
80
}
81
81
82
82
}
0 commit comments