Skip to content

Instantly share code, notes, and snippets.

@feulf
Created October 3, 2019 19:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save feulf/7ffa29083ed5a121869430cc4b1ba551 to your computer and use it in GitHub Desktop.
Save feulf/7ffa29083ed5a121869430cc4b1ba551 to your computer and use it in GitHub Desktop.
C code that shows the Poisson distribution of the probability to attack Bitcoin.
#include <math.h>
#include <stdio.h>
double AttackerSuccessProbability(double q, int z) {
double p = 1.0 - q;
double lambda = z * (q / p);
double sum = 1.0;
int i, k;
for (k = 0; k <= z; k++)
{
double poisson = exp(-lambda);
for (i = 1; i <= k; i++)
poisson *= lambda / i;
sum -= poisson * (1 - pow(q / p, z - k));
}
return sum;
}
int main() {
double P;
double q = 0.49;
for (int z = 0; z <= 1000; z++ ) {
P = AttackerSuccessProbability(q, z);
printf("q=%f z=%d P=%f\n", q, z, P);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment