Benford's law: Difference between revisions

Content added Content deleted
m (Move GW-BASIC into correct position, marked "needs improvement")
Line 3,266: Line 3,266:
9 4.500 4.576
9 4.500 4.576
</pre>
</pre>

=={{header|RPL}}==
Here we use an interesting RPL feature that allows to pass an arithmetic expression or a function name as an argument. Thus, the code below can check the 'benfordance' of various series without needing to edit it. The resulting array being a matrix, some additional operations allow to compute the maximum difference at digit level between the tested function and Benford's law, as a distance indicator.
{{works with|Halcyon Calc|4.2.7}}
≪ → sn min max
≪ { 9 2 } 0 CON
min max FOR j
{ 0 1 } 1 j sn EVAL
IF DUP THEN
MANT FLOOR PUT
DUP2 GET 1 + PUT
ELSE
3 DROPN
END
NEXT
max min - 1 + /
1 9 FOR j
{ 0 2 } 1 j PUT
j INV 1 + LOG PUT
NEXT
DUP [ 1 0 ] *
OVER [ 0 1 ] * -
RNRM
'BENFD' STO

'→FIB' 1 100 BENFD
≪ LOG ≫ 1 10000 BENFD
Output:
<pre>
4: [[ 0.301 0.301029995664 ]
[ 0.177 0.176091259056 ]
[ 0.125 0.124938736608 ]
[ 0.096 9.69100130081E-02 ]
[ 0.08 7.91812460476E-02 ]
[ 6.7E-02 6.69467896306E-02 ]
[ 0.056 5.79919469777E-02 ]
[ 0.053 5.11525224474E-02 ]
[ 0.045 4.57574905607E-02 ]]
3: 1.8847477552E-02
2: [[ 9E-04 0.301029995664 ]
[ 9E-03 0.176091259056 ]
[ 0.09001 0.124938736608 ]
[ 0.90001 9.69100130081E-02 ]
[ 0.00001 7.91812460476E-02 ]
[ 0.00002 6.69467896306E-02 ]
[ 0.00001 5.79919469777E-02 ]
[ 0.00001 5.11525224474E-02 ]
[ 0.00002 4.57574905607E-02 ]]
1: 0.775161263392
</pre>

=={{header|Ruby}}==
=={{header|Ruby}}==
{{trans|Python}}
{{trans|Python}}