Note on Benford’s Law
February 22nd, 2011While dealing with Benford’s Law i noticed, that the algorithm, which used the product formula, works more than 2 times faster, then the algorithm with the summation formula. Below you can see my two implementations of Benford’s Law in C. Thanks to Prof. Dr. Klaus Neumann for any suggestions.
Params
int n := position of the numeral in the number
int d := numeral
return := p(d) at position n
Example
1337 -> 1. benford(1,1) -> 2. benford(2,3) -> benford(3,3) -> benford(4,7)
Benford’s Law with summation formula
float benford10(int n, int d){float j = pow(10, n – 1), s = 0, i;for(i = floor(pow(10, (n – 2))); i <= j – 1; i++)s += log10(1 + (1 / ((i * 10) + d)));return s;}
Benford’s Law with product formula
float benford10(int n, int d){
float j = pow(10, n – 1) – 1, p = 1, i;
for(i = floor(pow(10, (n – 2))); i <= j; i++)
p *= (1 + (1/((10 * i) + d)));
return log10(p);
}
