pull down to refresh
105 sats \ 1 reply \ @nullama 11 Mar 2024 \ on: Taking a closer look at Satoshi's calculations written in C bitcoin_beginners
You have a small error as the function is defined inside the main().
Anyway, here is an online version that anyone can run from the web: https://glot.io/snippets/gu79r84jce
https://m.stacker.news/20050
My bad - I am not a C programmer. Here is a corrected version that follows the convention of writing proper C:
#include<stdio.h>
#include<math.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() {
//If the attacker is one block behind and there is 49% of dishones nodes
double prob = AttackerSuccessProbability(0.49, 1);
puts("The probability of the success of the attacker when they are 1 block behind the honest 51% majority of nodes:");
printf("%f\n", prob);
return 0;
}
The first version works just as well, but is not as elegant.
reply