Benford's law: Difference between revisions

→‎{{header|J}}: Table of expected vs actual freqs
(→‎{{header|J}}: Table of expected vs actual freqs)
Line 1,640:
</pre>
=={{header|J}}==
'''Solution'''
<syntaxhighlight lang="j">benford=: 10&^.@(1 + %) NB. expected frequency of 1-9 as first digit
Digits=: '123456789'
firstSigDigits=: {.@(-. -.&Digits)@":"0 NB. extract first significant digit from numbers
 
freq=: (] % +/)@:<:@(#/.~)@, NB. calc frequency of values (x) in y</syntaxhighlight>
'''Required Example'''
<syntaxhighlight lang="j"> First1000Fib=: (, +/@:(_2&{.)) ^: (1000-#) 1 1
NB. Expected vs Actual frequencies for Digits 1-9
Digits ((] ,. benford)@"."0@[ ,. (freq firstSigDigits)) First1000Fib
1 0.30103 0.301
2 0.176091 0.177
3 0.124939 0.125
4 0.09691 0.096
5 0.0791812 0.08
6 0.0669468 0.067
7 0.0579919 0.056
8 0.0511525 0.053
9 0.0457575 0.045</syntaxhighlight>
 
'''Alternatively'''
 
We show the correlation coefficient of Benford's law with the leading digits of the first 1000 Fibonacci numbers is almost unity.
<syntaxhighlight lang="j">log10 =: 10&^.
Line 1,677 ⟶ 1,699:
 
assert '0.9999' -: 6j4 ": TALLY_BY_KEY r benford >: i.9 NB. Of course we don't need normalization</syntaxhighlight>
 
=={{header|Java}}==
<syntaxhighlight lang="java">import java.math.BigInteger;
892

edits