Benford's law: Difference between revisions
Content added Content deleted
m (→{{header|Phix}}: flip rtn order) |
(Add VBA code) |
||
Line 3,779: | Line 3,779: | ||
9 | 4.50% | 4.58% |
9 | 4.50% | 4.58% |
||
</pre> |
</pre> |
||
=={{header|VBA (Visual Basic for Application)}}== |
|||
<lang vb> |
|||
Sub BenfordLaw() |
|||
Dim BenResult(1 To 9) As Long |
|||
BENref = "30,1%|17,6%|12,5%|9,7%|7,9%|6,7%|5,8%|5,1%|4,6%" |
|||
For Each c In Selection.Cells |
|||
If InStr(1, "-0123456789", Left(c, 1)) > 0 Then |
|||
For i = 1 To 9 |
|||
If CInt(Left(Abs(c), 1)) = i Then BenResult(i) = BenResult(i) + 1: Exit For |
|||
Next |
|||
End If |
|||
Next |
|||
Total= Application.Sum(BenResult) |
|||
biggest= Len(CStr(BenResult(1))) |
|||
txt = "# | Values | Real | Expected " & vbCrLf |
|||
For i = 1 To 9 |
|||
If BenResult(i) > 0 Then |
|||
txt = txt & "#" & i & " | " & vbTab |
|||
txt = txt & String((biggest - Len(CStr(BenResult(i)))) * 2, " ") & Format(BenResult(i), "0") & " | " & vbTab |
|||
txt = txt & String((Len(CStr(Format(BenResult(1) / Total, "##0.0%"))) - Len(CStr(Format(BenResult(i) / Total, "##0.0%")))) * 2, " ") & Format(BenResult(i) / Total, "##0.0%") & " | " & vbTab |
|||
txt = txt & Format(Split(BENref, "|")(i - 1), " ##0.0%") & vbCrLf |
|||
End If |
|||
Next |
|||
MsgBox txt, vbOKOnly, "Finish" |
|||
End Sub |
|||
} |
|||
</lang> |
|||
=={{header|Visual FoxPro}}== |
=={{header|Visual FoxPro}}== |