Skip to content

Instantly share code, notes, and snippets.

@jaymon0703
Created October 8, 2018 07:49
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 jaymon0703/c753626861c49509ce2d8eed5a7b0781 to your computer and use it in GitHub Desktop.
Save jaymon0703/c753626861c49509ce2d8eed5a7b0781 to your computer and use it in GitHub Desktop.
Satoshi Nakamoto's C code from the Bitcoin whitepaper for determining an attacker on the network's probability of success
#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;
int 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 ans = AttackerSuccessProbability(0.1, 1);
printf("'%f'", ans);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment