Decrease error rate for loop.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.