Benford's law: Difference between revisions
Content added Content deleted
(Add F# version) |
|||
Line 1,144: | Line 1,144: | ||
8 0.053 0.05115252244738129 |
8 0.053 0.05115252244738129 |
||
9 0.045 0.04575749056067514 |
9 0.045 0.04575749056067514 |
||
</pre> |
|||
=={{header|F sharp|F#}}== |
|||
For Fibonacci code, see https://rosettacode.org/wiki/Fibonacci_sequence#F.89 |
|||
<lang fsharp>open System |
|||
let fibonacci = Seq.unfold (fun (x, y) -> Some(x, (y, x + y))) (0I,1I) |
|||
let fibFirstNumbers nth = |
|||
fibonacci |> Seq.take nth |> Seq.map (fun n -> n.ToString().[0] |> string |> Int32.Parse) |
|||
let fibFirstNumbersFrequency nth = |
|||
let firstNumbers = fibFirstNumbers nth |> Seq.toList |
|||
let counts = firstNumbers |> List.countBy id |> List.sort |> List.filter (fun (k, _) -> k <> 0) |
|||
let total = firstNumbers |> List.length |> float |
|||
counts |> List.map (fun (_, v) -> float v/total) |
|||
let benfordLaw d = log10(1.0 + (1.0/float d)) |
|||
let benfordLawFigures = [1..9] |> List.map benfordLaw |
|||
let run () = |
|||
printfn "Frequency of the first digit (1 through 9) in the Fibonacci sequence:" |
|||
fibFirstNumbersFrequency 1000 |> List.iter (fun f -> printf $"{f:N5} ") |
|||
printfn "\nBenford's law for 1 through 9:" |
|||
benfordLawFigures |> List.iter (fun f -> printf $"{f:N5} ") |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
Frequency of the first digit (1 through 9) in the Fibonacci sequence: |
|||
0.30100 0.17700 0.12500 0.09500 0.08000 0.06700 0.05600 0.05300 0.04500 |
|||
Benford's law for 1 through 9: |
|||
0.30103 0.17609 0.12494 0.09691 0.07918 0.06695 0.05799 0.05115 0.04576 |
|||
</pre> |
</pre> |
||