Quesiton:
Given a method
Long compute (int i) {return ...}
The error rate P = 1/10,000
Another method
Long total (int n) {long s = 0; for (i = 0; i < n; i + +) {s + = compute (i); } return s;}
Thus the error rate is NP.
How to improve the second method to control it rate below p.
Total would run n steps. So-to-make the below p.
Each step need to below P/N.
Let's run compute K times when
(p ^ k < p/n).
Long total (int n) { int k = calccomputetimes (n); long s = 0; for (int i = 0 ; i < n ; i++) { s += safecompute (i, k); } Return s;} Find the min value of k when P^k < p/nint Calccomputetimes (n) { double temp = p / n; int k = 1; while (Pow (p, k) >= temp) { k ++; } return k;} runs compute () k times to find the most possible results. If the possible of two results are the same, return The first computed one.double safecompute (i, k) {&Nbsp; map<double, integer> computemap = new hashmap<> (); int maxoccurseen = 0; double result = 0; for (int t = 0 ; t < k ; t ++) { int r = compute (i); integer occur = computemap.get ( R); if (occur == null) { occur = 1; } computemap.put (R, occur) ; if (Occur > maxoccurseen) { maxOccurSeen = occur; result = R; } } return result;} static double p = 1 / 10,000;
Decrease error rate for loop.