This is a quote from the book "Bitcoin: A Peer-to-Peer Electronic Cash System" by Satoshi Nakamoto
...on density for each amount of progress he could have made by the probability he could catch up from that point:
read full book
to avoid summing the infinite tail of the distribution...
Converting to C code...
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++)