Benford's law: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
Thundergnat (talk | contribs) m (Automated syntax highlighting fixup (second round - minor fixes)) |
||
Line 31: | Line 31: | ||
* A starting page on Wolfram Mathworld is {{Wolfram|Benfords|Law}}. |
* A starting page on Wolfram Mathworld is {{Wolfram|Benfords|Law}}. |
||
<br><br> |
<br><br> |
||
=={{header|11l}}== |
|||
{{trans|D}} |
|||
<syntaxhighlight lang=11l>F get_fibs() |
|||
V a = 1.0 |
|||
V b = 1.0 |
|||
[Float] r |
|||
L 1000 |
|||
r [+]= a |
|||
(a, b) = (b, a + b) |
|||
R r |
|||
F benford(seq) |
|||
V freqs = [(0.0, 0.0)] * 9 |
|||
V seq_len = 0 |
|||
L(d) seq |
|||
I d != 0 |
|||
freqs[String(d)[0].code - ‘1’.code][1]++ |
|||
seq_len++ |
|||
L(&f) freqs |
|||
f = (log10(1.0 + 1.0 / (L.index + 1)), f[1] / seq_len) |
|||
R freqs |
|||
print(‘#9 #9 #9’.format(‘Actual’, ‘Expected’, ‘Deviation’)) |
|||
L(p) benford(get_fibs()) |
|||
print(‘#.: #2.2% | #2.2% | #.4%’.format(L.index + 1, p[1] * 100, p[0] * 100, abs(p[1] - p[0]) * 100))</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Actual Expected Deviation |
|||
1: 30.10% | 30.10% | 0.0030% |
|||
2: 17.70% | 17.61% | 0.0909% |
|||
3: 12.50% | 12.49% | 0.0061% |
|||
4: 9.60% | 9.69% | 0.0910% |
|||
5: 8.00% | 7.92% | 0.0819% |
|||
6: 6.70% | 6.69% | 0.0053% |
|||
7: 5.60% | 5.80% | 0.1992% |
|||
8: 5.30% | 5.12% | 0.1847% |
|||
9: 4.50% | 4.58% | 0.0757% |
|||
</pre> |
|||
=={{header|8th}}== |
=={{header|8th}}== |
||
<syntaxhighlight lang=8th> |
<syntaxhighlight lang="8th"> |
||
: n:log10e ` 1 10 ln / ` ; |
: n:log10e ` 1 10 ln / ` ; |
||
Line 157: | Line 115: | ||
9 0.0458 0.045 |
9 0.0458 0.045 |
||
</pre> |
</pre> |
||
=={{header|11l}}== |
|||
{{trans|D}} |
|||
<syntaxhighlight lang="11l">F get_fibs() |
|||
V a = 1.0 |
|||
V b = 1.0 |
|||
[Float] r |
|||
L 1000 |
|||
r [+]= a |
|||
(a, b) = (b, a + b) |
|||
R r |
|||
F benford(seq) |
|||
V freqs = [(0.0, 0.0)] * 9 |
|||
V seq_len = 0 |
|||
L(d) seq |
|||
I d != 0 |
|||
freqs[String(d)[0].code - ‘1’.code][1]++ |
|||
seq_len++ |
|||
L(&f) freqs |
|||
f = (log10(1.0 + 1.0 / (L.index + 1)), f[1] / seq_len) |
|||
R freqs |
|||
print(‘#9 #9 #9’.format(‘Actual’, ‘Expected’, ‘Deviation’)) |
|||
L(p) benford(get_fibs()) |
|||
print(‘#.: #2.2% | #2.2% | #.4%’.format(L.index + 1, p[1] * 100, p[0] * 100, abs(p[1] - p[0]) * 100))</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Actual Expected Deviation |
|||
1: 30.10% | 30.10% | 0.0030% |
|||
2: 17.70% | 17.61% | 0.0909% |
|||
3: 12.50% | 12.49% | 0.0061% |
|||
4: 9.60% | 9.69% | 0.0910% |
|||
5: 8.00% | 7.92% | 0.0819% |
|||
6: 6.70% | 6.69% | 0.0053% |
|||
7: 5.60% | 5.80% | 0.1992% |
|||
8: 5.30% | 5.12% | 0.1847% |
|||
9: 4.50% | 4.58% | 0.0757% |
|||
</pre> |
|||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
The program reads the Fibonacci-Numbers from the standard input. Each input line is supposed to hold N, followed by Fib(N). |
The program reads the Fibonacci-Numbers from the standard input. Each input line is supposed to hold N, followed by Fib(N). |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="ada">with Ada.Text_IO, Ada.Numerics.Generic_Elementary_Functions; |
||
procedure Benford is |
procedure Benford is |
||
Line 230: | Line 227: | ||
Input is the list of primes below 100,000 from [http://www.mathsisfun.com/numbers/prime-number-lists.html]. Since each line in that file holds prime and only a prime, but no ongoing counter, we must slightly modify the program by commenting out a single line: |
Input is the list of primes below 100,000 from [http://www.mathsisfun.com/numbers/prime-number-lists.html]. Since each line in that file holds prime and only a prime, but no ongoing counter, we must slightly modify the program by commenting out a single line: |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="ada"> -- N_IO.Get(Counter);</syntaxhighlight> |
||
We can also edit out the declaration of the variable "Counter" ...or live with a compiler warning about never reading or assigning that variable. |
We can also edit out the declaration of the variable "Counter" ...or live with a compiler warning about never reading or assigning that variable. |
||
Line 249: | Line 246: | ||
8 1003 490.7 10.46 5.12 5.34 |
8 1003 490.7 10.46 5.12 5.34 |
||
9 1006 438.9 10.49 4.58 5.91</pre> |
9 1006 438.9 10.49 4.58 5.91</pre> |
||
=={{header|Aime}}== |
=={{header|Aime}}== |
||
<syntaxhighlight lang=aime>text |
<syntaxhighlight lang="aime">text |
||
sum(text a, text b) |
sum(text a, text b) |
||
{ |
{ |
||
Line 350: | Line 346: | ||
8 5.115 5.300 |
8 5.115 5.300 |
||
9 4.575 4.5 </pre> |
9 4.575 4.5 </pre> |
||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}} |
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}} |
||
Uses Algol 68G's LONG LONG INT which has programmer specifiable precision. |
Uses Algol 68G's LONG LONG INT which has programmer specifiable precision. |
||
<syntaxhighlight lang=algol68>BEGIN |
<syntaxhighlight lang="algol68">BEGIN |
||
# set the number of digits for LONG LONG INT values # |
# set the number of digits for LONG LONG INT values # |
||
PR precision 256 PR |
PR precision 256 PR |
||
Line 403: | Line 398: | ||
Benford: 0.046 actual: 0.045 |
Benford: 0.046 actual: 0.045 |
||
</pre> |
</pre> |
||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
{{works with|AutoHotkey_L}}(AutoHotkey1.1+) |
{{works with|AutoHotkey_L}}(AutoHotkey1.1+) |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="autohotkey">SetBatchLines, -1 |
||
fib := NStepSequence(1, 1, 2, 1000) |
fib := NStepSequence(1, 1, 2, 1000) |
||
Out := "Digit`tExpected`tObserved`tDeviation`n" |
Out := "Digit`tExpected`tObserved`tDeviation`n" |
||
Line 460: | Line 454: | ||
8 5.115252 5.300000 0.184748 |
8 5.115252 5.300000 0.184748 |
||
9 4.575749 4.500000 0.075749</pre> |
9 4.575749 4.500000 0.075749</pre> |
||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="awk"> |
||
# syntax: GAWK -f BENFORDS_LAW.AWK |
# syntax: GAWK -f BENFORDS_LAW.AWK |
||
BEGIN { |
BEGIN { |
||
Line 504: | Line 497: | ||
9 4.5757 4.5000 0.0757 |
9 4.5757 4.5000 0.0757 |
||
</pre> |
</pre> |
||
=={{header|BCPL}}== |
=={{header|BCPL}}== |
||
BCPL doesn't do floating point well, so I use integer math to compute the most significant digits of the Fibonacci sequence and use a table that has the values of log10(d + 1/d) |
BCPL doesn't do floating point well, so I use integer math to compute the most significant digits of the Fibonacci sequence and use a table that has the values of log10(d + 1/d) |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="bcpl"> |
||
GET "libhdr" |
GET "libhdr" |
||
Line 566: | Line 558: | ||
9 0.045 0.046 |
9 0.045 0.046 |
||
</pre> |
</pre> |
||
=={{header|C}}== |
=={{header|C}}== |
||
<syntaxhighlight lang=c>#include <stdio.h> |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
#include <math.h> |
#include <math.h> |
||
Line 646: | Line 637: | ||
8 0.053 0.051 |
8 0.053 0.051 |
||
9 0.045 0.046</pre> |
9 0.045 0.046</pre> |
||
=={{header|C++}}== |
=={{header|C++}}== |
||
<syntaxhighlight lang=cpp>//to cope with the big numbers , I used the Class Library for Numbers( CLN ) |
<syntaxhighlight lang="cpp">//to cope with the big numbers , I used the Class Library for Numbers( CLN ) |
||
//if used prepackaged you can compile writing "g++ -std=c++11 -lcln yourprogram.cpp -o yourprogram" |
//if used prepackaged you can compile writing "g++ -std=c++11 -lcln yourprogram.cpp -o yourprogram" |
||
#include <cln/integer.h> |
#include <cln/integer.h> |
||
Line 723: | Line 713: | ||
9 : 4.5 % 4.58 % |
9 : 4.5 % 4.58 % |
||
</pre> |
</pre> |
||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
<syntaxhighlight lang=lisp>(ns example |
<syntaxhighlight lang="lisp">(ns example |
||
(:gen-class)) |
(:gen-class)) |
||
Line 836: | Line 825: | ||
9 5.76 4.58 1.18 |
9 5.76 4.58 1.18 |
||
</pre> |
</pre> |
||
=={{header|CoffeeScript}}== |
=={{header|CoffeeScript}}== |
||
<syntaxhighlight lang=coffeescript>fibgen = () -> |
<syntaxhighlight lang="coffeescript">fibgen = () -> |
||
a = 1; b = 0 |
a = 1; b = 0 |
||
return () -> |
return () -> |
||
Line 871: | Line 859: | ||
8 0.053 0.051 |
8 0.053 0.051 |
||
9 0.045 0.046</pre> |
9 0.045 0.046</pre> |
||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
<syntaxhighlight lang=lisp>(defun calculate-distribution (numbers) |
<syntaxhighlight lang="lisp">(defun calculate-distribution (numbers) |
||
"Return the frequency distribution of the most significant nonzero |
"Return the frequency distribution of the most significant nonzero |
||
digits in the given list of numbers. The first element of the list |
digits in the given list of numbers. The first element of the list |
||
Line 929: | Line 916: | ||
8 0.053 0.051 |
8 0.053 0.051 |
||
9 0.045 0.046</pre> |
9 0.045 0.046</pre> |
||
=={{header|Crystal}}== |
=={{header|Crystal}}== |
||
{{trans|Ruby}} |
{{trans|Ruby}} |
||
<syntaxhighlight lang=ruby>require "big" |
<syntaxhighlight lang="ruby">require "big" |
||
EXPECTED = (1..9).map{ |d| Math.log10(1 + 1.0 / d) } |
EXPECTED = (1..9).map{ |d| Math.log10(1 + 1.0 / d) } |
||
Line 1,007: | Line 993: | ||
9: 12.3% 4.6% 7.7% |
9: 12.3% 4.6% 7.7% |
||
</pre> |
</pre> |
||
=={{header|D}}== |
=={{header|D}}== |
||
{{trans|Scala}} |
{{trans|Scala}} |
||
<syntaxhighlight lang=d>import std.stdio, std.range, std.math, std.conv, std.bigint; |
<syntaxhighlight lang="d">import std.stdio, std.range, std.math, std.conv, std.bigint; |
||
double[2][9] benford(R)(R seq) if (isForwardRange!R && !isInfinite!R) { |
double[2][9] benford(R)(R seq) if (isForwardRange!R && !isInfinite!R) { |
||
Line 1,049: | Line 1,034: | ||
===Alternative Version=== |
===Alternative Version=== |
||
The output is the same. |
The output is the same. |
||
<syntaxhighlight lang=d>import std.stdio, std.range, std.math, std.conv, std.bigint, |
<syntaxhighlight lang="d">import std.stdio, std.range, std.math, std.conv, std.bigint, |
||
std.algorithm, std.array; |
std.algorithm, std.array; |
||
Line 1,070: | Line 1,055: | ||
See [https://rosettacode.org/wiki/Benford%27s_law#Pascal Pascal]. |
See [https://rosettacode.org/wiki/Benford%27s_law#Pascal Pascal]. |
||
=={{header|Elixir}}== |
=={{header|Elixir}}== |
||
<syntaxhighlight lang=elixir>defmodule Benfords_law do |
<syntaxhighlight lang="elixir">defmodule Benfords_law do |
||
def distribution(n), do: :math.log10( 1 + (1 / n) ) |
def distribution(n), do: :math.log10( 1 + (1 / n) ) |
||
Line 1,102: | Line 1,087: | ||
9 0.045 0.04575749056067514 |
9 0.045 0.04575749056067514 |
||
</pre> |
</pre> |
||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="erlang"> |
||
-module( benfords_law ). |
-module( benfords_law ). |
||
-export( [actual_distribution/1, distribution/1, task/0] ). |
-export( [actual_distribution/1, distribution/1, task/0] ). |
||
Line 1,145: | Line 1,129: | ||
9 0.045 0.04575749056067514 |
9 0.045 0.04575749056067514 |
||
</pre> |
</pre> |
||
=={{header|F sharp|F#}}== |
=={{header|F sharp|F#}}== |
||
For Fibonacci code, see https://rosettacode.org/wiki/Fibonacci_sequence#F.89 |
For Fibonacci code, see https://rosettacode.org/wiki/Fibonacci_sequence#F.89 |
||
<syntaxhighlight lang=fsharp>open System |
<syntaxhighlight lang="fsharp">open System |
||
let fibonacci = Seq.unfold (fun (x, y) -> Some(x, (y, x + y))) (0I,1I) |
let fibonacci = Seq.unfold (fun (x, y) -> Some(x, (y, x + y))) (0I,1I) |
||
Line 1,180: | Line 1,163: | ||
</pre> |
</pre> |
||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
<syntaxhighlight lang=factor>USING: assocs compiler.tree.propagation.call-effect formatting |
<syntaxhighlight lang="factor">USING: assocs compiler.tree.propagation.call-effect formatting |
||
kernel math math.functions math.statistics math.text.utils |
kernel math math.functions math.statistics math.text.utils |
||
sequences ; |
sequences ; |
||
Line 1,224: | Line 1,206: | ||
9 0.0458 0.0450 |
9 0.0458 0.0450 |
||
</pre> |
</pre> |
||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
<syntaxhighlight lang=forth>: 3drop drop 2drop ; |
<syntaxhighlight lang="forth">: 3drop drop 2drop ; |
||
: f2drop fdrop fdrop ; |
: f2drop fdrop fdrop ; |
||
Line 1,286: | Line 1,267: | ||
8 0.053 0.0512 |
8 0.053 0.0512 |
||
9 0.045 0.0458 ok</pre> |
9 0.045 0.0458 ok</pre> |
||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
FORTRAN 90. Compilation and output of this program using emacs compile command and a fairly obvious Makefile entry: |
FORTRAN 90. Compilation and output of this program using emacs compile command and a fairly obvious Makefile entry: |
||
<syntaxhighlight lang=fortran>-*- mode: compilation; default-directory: "/tmp/" -*- |
<syntaxhighlight lang="fortran">-*- mode: compilation; default-directory: "/tmp/" -*- |
||
Compilation started at Sat May 18 01:13:00 |
Compilation started at Sat May 18 01:13:00 |
||
Line 1,299: | Line 1,279: | ||
Compilation finished at Sat May 18 01:13:00</syntaxhighlight> |
Compilation finished at Sat May 18 01:13:00</syntaxhighlight> |
||
<syntaxhighlight lang=fortran>subroutine fibber(a,b,c,d) |
<syntaxhighlight lang="fortran">subroutine fibber(a,b,c,d) |
||
! compute most significant digits, Fibonacci like. |
! compute most significant digits, Fibonacci like. |
||
implicit none |
implicit none |
||
Line 1,368: | Line 1,348: | ||
write(6,*) (count(i)/1000.0 ,i=1,9),'LEADING FIBONACCI DIGIT' |
write(6,*) (count(i)/1000.0 ,i=1,9),'LEADING FIBONACCI DIGIT' |
||
end program benford</syntaxhighlight> |
end program benford</syntaxhighlight> |
||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
{{libheader|GMP}} |
{{libheader|GMP}} |
||
<syntaxhighlight lang=freebasic>' version 27-10-2016 |
<syntaxhighlight lang="freebasic">' version 27-10-2016 |
||
' compile with: fbc -s console |
' compile with: fbc -s console |
||
Line 1,488: | Line 1,467: | ||
8 71038 7.10 % 5.12 % -1.989 % |
8 71038 7.10 % 5.12 % -1.989 % |
||
9 70320 7.03 % 4.58 % -2.456 %</pre> |
9 70320 7.03 % 4.58 % -2.456 %</pre> |
||
=={{header|Fōrmulæ}}== |
=={{header|Fōrmulæ}}== |
||
Line 1,496: | Line 1,474: | ||
In '''[https://formulae.org/?example=Benford%27s_law this]''' page you can see the program(s) related to this task and their results. |
In '''[https://formulae.org/?example=Benford%27s_law this]''' page you can see the program(s) related to this task and their results. |
||
=={{header|Go}}== |
=={{header|Go}}== |
||
<syntaxhighlight lang=go>package main |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 1,543: | Line 1,520: | ||
9 0.045 0.046 |
9 0.045 0.046 |
||
</pre> |
</pre> |
||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
'''Solution:'''<br> |
'''Solution:'''<br> |
||
Uses [[Fibonacci_sequence#Analytic_8|Fibonacci sequence analytic formula]] |
Uses [[Fibonacci_sequence#Analytic_8|Fibonacci sequence analytic formula]] |
||
{{trans|Java}} |
{{trans|Java}} |
||
<syntaxhighlight lang=groovy>def tallyFirstDigits = { size, generator -> |
<syntaxhighlight lang="groovy">def tallyFirstDigits = { size, generator -> |
||
def population = (0..<size).collect { generator(it) } |
def population = (0..<size).collect { generator(it) } |
||
def firstDigits = [0]*10 |
def firstDigits = [0]*10 |
||
Line 1,558: | Line 1,534: | ||
'''Test:''' |
'''Test:''' |
||
<syntaxhighlight lang=groovy>def digitCounts = tallyFirstDigits(1000, aFib) |
<syntaxhighlight lang="groovy">def digitCounts = tallyFirstDigits(1000, aFib) |
||
println "d actual predicted" |
println "d actual predicted" |
||
(1..<10).each { |
(1..<10).each { |
||
Line 1,575: | Line 1,551: | ||
8 0.053000 0.051153 |
8 0.053000 0.051153 |
||
9 0.045000 0.045757</pre> |
9 0.045000 0.045757</pre> |
||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
<syntaxhighlight lang=haskell>import qualified Data.Map as M |
<syntaxhighlight lang="haskell">import qualified Data.Map as M |
||
import Data.Char (digitToInt) |
import Data.Char (digitToInt) |
||
Line 1,610: | Line 1,585: | ||
(9,0.045,0.0457574905606751)] |
(9,0.045,0.0457574905606751)] |
||
</pre> |
</pre> |
||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |
||
The following solution works in both languages. |
The following solution works in both languages. |
||
<syntaxhighlight lang=unicon>global counts, total |
<syntaxhighlight lang="unicon">global counts, total |
||
procedure main() |
procedure main() |
||
Line 1,665: | Line 1,639: | ||
-> |
-> |
||
</pre> |
</pre> |
||
=={{header|J}}== |
=={{header|J}}== |
||
We show the correlation coefficient of Benford's law with the leading digits of the first 1000 Fibonacci numbers is almost unity. |
We show the correlation coefficient of Benford's law with the leading digits of the first 1000 Fibonacci numbers is almost unity. |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="j">log10 =: 10&^. |
||
benford =: log10@:(1+%) |
benford =: log10@:(1+%) |
||
assert '0.30 0.18 0.12 0.10 0.08 0.07 0.06 0.05 0.05' -: 5j2 ": benford >: i. 9 |
assert '0.30 0.18 0.12 0.10 0.08 0.07 0.06 0.05 0.05' -: 5j2 ": benford >: i. 9 |
||
Line 1,704: | Line 1,677: | ||
assert '0.9999' -: 6j4 ": TALLY_BY_KEY r benford >: i.9 NB. Of course we don't need normalization</syntaxhighlight> |
assert '0.9999' -: 6j4 ": TALLY_BY_KEY r benford >: i.9 NB. Of course we don't need normalization</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="java">import java.math.BigInteger; |
||
import java.util.Locale; |
import java.util.Locale; |
||
Line 1,746: | Line 1,718: | ||
9 0.045000 0.045757</pre> |
9 0.045000 0.045757</pre> |
||
To use other number sequences, implement a suitable <tt>NumberGenerator</tt>, construct a <tt>Benford</tt> instance with it and print it. |
To use other number sequences, implement a suitable <tt>NumberGenerator</tt>, construct a <tt>Benford</tt> instance with it and print it. |
||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
<syntaxhighlight lang=javascript>const fibseries = n => [...Array(n)] |
<syntaxhighlight lang="javascript">const fibseries = n => [...Array(n)] |
||
.reduce( |
.reduce( |
||
(fib, _, i) => i < 2 ? ( |
(fib, _, i) => i < 2 ? ( |
||
Line 1,777: | Line 1,748: | ||
7: (3) [8, 0.053, 0.05115252244738129] |
7: (3) [8, 0.053, 0.05115252244738129] |
||
8: (3) [9, 0.045, 0.04575749056067514]</pre> |
8: (3) [9, 0.045, 0.04575749056067514]</pre> |
||
=={{header|jq}}== |
=={{header|jq}}== |
||
{{works with|jq|1.4}} |
{{works with|jq|1.4}} |
||
This implementation shows the observed and expected number of occurrences together with the χ² statistic. |
This implementation shows the observed and expected number of occurrences together with the χ² statistic. |
||
For the sake of being self-contained, the following includes a generator for Fibonacci numbers, and a prime number generator that is inefficient but brief and can generate numbers within an arbitrary range.<syntaxhighlight lang=jq># Generate the first n Fibonacci numbers: 1, 1, ... |
For the sake of being self-contained, the following includes a generator for Fibonacci numbers, and a prime number generator that is inefficient but brief and can generate numbers within an arbitrary range.<syntaxhighlight lang="jq"># Generate the first n Fibonacci numbers: 1, 1, ... |
||
# Numerical accuracy is insufficient beyond about 1450. |
# Numerical accuracy is insufficient beyond about 1450. |
||
def fibonacci(n): |
def fibonacci(n): |
||
Line 1,959: | Line 1,929: | ||
χ² = 3204.8072</pre> |
χ² = 3204.8072</pre> |
||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="julia">fib(n) = ([one(n) one(n) ; one(n) zero(n)]^n)[1,2] |
||
ben(l) = [count(x->x==i, map(n->string(n)[1],l)) for i='1':'9']./length(l) |
ben(l) = [count(x->x==i, map(n->string(n)[1],l)) for i='1':'9']./length(l) |
||
Line 1,980: | Line 1,949: | ||
9 0.045 0.0457575 |
9 0.045 0.0457575 |
||
</pre> |
</pre> |
||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
<syntaxhighlight lang=scala>import java.math.BigInteger |
<syntaxhighlight lang="scala">import java.math.BigInteger |
||
interface NumberGenerator { |
interface NumberGenerator { |
||
Line 2,021: | Line 1,989: | ||
fun main(a: Array<String>) = println(Benford(FibonacciGenerator))</syntaxhighlight> |
fun main(a: Array<String>) = println(Benford(FibonacciGenerator))</syntaxhighlight> |
||
=={{header|Liberty BASIC}}== |
=={{header|Liberty BASIC}}== |
||
Using function from |
Using function from |
||
http://rosettacode.org/wiki/Fibonacci_sequence#Liberty_BASIC |
http://rosettacode.org/wiki/Fibonacci_sequence#Liberty_BASIC |
||
<syntaxhighlight lang=lb> |
<syntaxhighlight lang="lb"> |
||
dim bin(9) |
dim bin(9) |
||
Line 2,076: | Line 2,043: | ||
9 0.045 0.046 |
9 0.045 0.046 |
||
</pre> |
</pre> |
||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
<syntaxhighlight lang=lua>actual = {} |
<syntaxhighlight lang="lua">actual = {} |
||
expected = {} |
expected = {} |
||
for i = 1, 9 do |
for i = 1, 9 do |
||
Line 2,109: | Line 2,075: | ||
8 0.053 0.051152522447381 |
8 0.053 0.051152522447381 |
||
9 0.045 0.045757490560675</pre> |
9 0.045 0.045757490560675</pre> |
||
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
||
<syntaxhighlight lang=mathematica>fibdata = Array[First@IntegerDigits@Fibonacci@# &, 1000]; |
<syntaxhighlight lang="mathematica">fibdata = Array[First@IntegerDigits@Fibonacci@# &, 1000]; |
||
Table[{d, N@Count[fibdata, d]/Length@fibdata, Log10[1. + 1/d]}, {d, 1, |
Table[{d, N@Count[fibdata, d]/Length@fibdata, Log10[1. + 1/d]}, {d, 1, |
||
9}] // Grid</syntaxhighlight> |
9}] // Grid</syntaxhighlight> |
||
Line 2,124: | Line 2,089: | ||
8 0.053 0.0511525 |
8 0.053 0.0511525 |
||
9 0.045 0.0457575</pre> |
9 0.045 0.0457575</pre> |
||
=={{header|NetRexx}}== |
=={{header|NetRexx}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="netrexx">/* NetRexx */ |
||
options replace format comments java crossref symbols nobinary |
options replace format comments java crossref symbols nobinary |
||
Line 2,182: | Line 2,146: | ||
9: 4.500000% 4.575749% 0.075749% |
9: 4.500000% 4.575749% 0.075749% |
||
</pre> |
</pre> |
||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="nim">import math |
||
import strformat |
import strformat |
||
Line 2,276: | Line 2,239: | ||
8 0.0530 0.0512 |
8 0.0530 0.0512 |
||
9 0.0450 0.0458</pre> |
9 0.0450 0.0458</pre> |
||
=={{header|Oberon-2}}== |
=={{header|Oberon-2}}== |
||
{{Works with|oo2c version 2}} |
{{Works with|oo2c version 2}} |
||
<syntaxhighlight lang=oberon2> |
<syntaxhighlight lang="oberon2"> |
||
MODULE BenfordLaw; |
MODULE BenfordLaw; |
||
IMPORT |
IMPORT |
||
Line 2,338: | Line 2,300: | ||
9 0.045 0.046 |
9 0.045 0.046 |
||
</pre> |
</pre> |
||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
For the Fibonacci sequence, we use the function from |
For the Fibonacci sequence, we use the function from |
||
https://rosettacode.org/wiki/Fibonacci_sequence#Arbitrary_Precision<br> |
https://rosettacode.org/wiki/Fibonacci_sequence#Arbitrary_Precision<br> |
||
Note the remark about the compilation of the program there. |
Note the remark about the compilation of the program there. |
||
<syntaxhighlight lang=ocaml> |
<syntaxhighlight lang="ocaml"> |
||
open Num |
open Num |
||
Line 2,382: | Line 2,343: | ||
0.301030 0.176091 0.124939 0.096910 0.079181 0.066947 0.057992 0.051153 0.045757 |
0.301030 0.176091 0.124939 0.096910 0.079181 0.066947 0.057992 0.051153 0.045757 |
||
</pre> |
</pre> |
||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
<syntaxhighlight lang=parigp>distribution(v)={ |
<syntaxhighlight lang="parigp">distribution(v)={ |
||
my(t=vector(9,n,sum(i=1,#v,v[i]==n))); |
my(t=vector(9,n,sum(i=1,#v,v[i]==n))); |
||
print("Digit\tActual\tExpected"); |
print("Digit\tActual\tExpected"); |
||
Line 2,415: | Line 2,375: | ||
8 51 51 |
8 51 51 |
||
9 46 46</pre> |
9 46 46</pre> |
||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
<syntaxhighlight lang=pascal>program fibFirstdigit; |
<syntaxhighlight lang="pascal">program fibFirstdigit; |
||
{$IFDEF FPC}{$MODE Delphi}{$ELSE}{$APPTYPE CONSOLE}{$ENDIF} |
{$IFDEF FPC}{$MODE Delphi}{$ELSE}{$APPTYPE CONSOLE}{$ENDIF} |
||
uses |
uses |
||
Line 2,481: | Line 2,440: | ||
8 53 51 -3.92157 % |
8 53 51 -3.92157 % |
||
9 45 45 0.00000 %</pre> |
9 45 45 0.00000 %</pre> |
||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="perl">#!/usr/bin/perl |
||
use strict ; |
use strict ; |
||
use warnings ; |
use warnings ; |
||
Line 2,521: | Line 2,479: | ||
9 : 4.50 % 4.58 % |
9 : 4.50 % 4.58 % |
||
</pre> |
</pre> |
||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
<!--<syntaxhighlight lang= |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
<span style="color: #008080;">function</span> <span style="color: #000000;">benford</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">string</span> <span style="color: #000000;">title</span><span style="color: #0000FF;">)</span> |
<span style="color: #008080;">function</span> <span style="color: #000000;">benford</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">string</span> <span style="color: #000000;">title</span><span style="color: #0000FF;">)</span> |
||
Line 2,569: | Line 2,526: | ||
9 4.500 4.576 9 10.060 4.576 9 4.600 4.576 |
9 4.500 4.576 9 10.060 4.576 9 4.600 4.576 |
||
</pre> |
</pre> |
||
=={{header|Picat}}== |
=={{header|Picat}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="picat">go => |
||
N = 1000, |
N = 1000, |
||
Line 2,630: | Line 2,586: | ||
8: count= 11 observed: 5.14% Benford: 5.12% diff=0.025 |
8: count= 11 observed: 5.14% Benford: 5.12% diff=0.025 |
||
9: count= 4 observed: 1.87% Benford: 4.58% diff=2.707</pre> |
9: count= 4 observed: 1.87% Benford: 4.58% diff=2.707</pre> |
||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
Picolisp does not support floating point math, but it can call libc math functions and convert the results to a fixed point number for e.g. natural logarithm. |
Picolisp does not support floating point math, but it can call libc math functions and convert the results to a fixed point number for e.g. natural logarithm. |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="picolisp"> |
||
(scl 4) |
(scl 4) |
||
(load "@lib/misc.l") |
(load "@lib/misc.l") |
||
Line 2,685: | Line 2,640: | ||
9 0.045 0.046 |
9 0.045 0.046 |
||
</pre> |
</pre> |
||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="pl/i"> |
||
(fofl, size, subrg): |
(fofl, size, subrg): |
||
Benford: procedure options(main); /* 20 October 2013 */ |
Benford: procedure options(main); /* 20 October 2013 */ |
||
Line 2,741: | Line 2,695: | ||
9 0.04575749 0.04499817 |
9 0.04575749 0.04499817 |
||
</pre> |
</pre> |
||
=={{header|PL/pgSQL}}== |
=={{header|PL/pgSQL}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="sql"> |
||
WITH recursive |
WITH recursive |
||
constant(val) AS |
constant(val) AS |
||
Line 2,779: | Line 2,732: | ||
from benford) c |
from benford) c |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
The sample file was not found. I selected another that contained the first two-thousand in the Fibonacci sequence, so there is a small amount of extra filtering. |
The sample file was not found. I selected another that contained the first two-thousand in the Fibonacci sequence, so there is a small amount of extra filtering. |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="powershell"> |
||
$url = "https://oeis.org/A000045/b000045.txt" |
$url = "https://oeis.org/A000045/b000045.txt" |
||
$file = "$env:TEMP\FibonacciNumbers.txt" |
$file = "$env:TEMP\FibonacciNumbers.txt" |
||
Line 2,814: | Line 2,766: | ||
9 45 0.045 0.04576 |
9 45 0.045 0.04576 |
||
</pre> |
</pre> |
||
=={{header|Prolog}}== |
=={{header|Prolog}}== |
||
{{works with|SWI Prolog|6.2.6 by Jan Wielemaker, University of Amsterdam}} |
{{works with|SWI Prolog|6.2.6 by Jan Wielemaker, University of Amsterdam}} |
||
Note: SWI Prolog implements arbitrary precision integer arithmetic through use of the GNU MP library |
Note: SWI Prolog implements arbitrary precision integer arithmetic through use of the GNU MP library |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="prolog">%_________________________________________________________________ |
||
% Does the Fibonacci sequence follow Benford's law? |
% Does the Fibonacci sequence follow Benford's law? |
||
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||
Line 2,874: | Line 2,825: | ||
5.12% - 5.30% |
5.12% - 5.30% |
||
4.58% - 4.50%</pre> |
4.58% - 4.50%</pre> |
||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
<syntaxhighlight lang=purebasic>#MAX_N=1000 |
<syntaxhighlight lang="purebasic">#MAX_N=1000 |
||
NewMap d1.i() |
NewMap d1.i() |
||
Dim fi.s(#MAX_N) |
Dim fi.s(#MAX_N) |
||
Line 2,930: | Line 2,880: | ||
Press Enter...</pre> |
Press Enter...</pre> |
||
=={{header|Python}}== |
=={{header|Python}}== |
||
Works with Python 3.X & 2.7 |
Works with Python 3.X & 2.7 |
||
<syntaxhighlight lang=python>from __future__ import division |
<syntaxhighlight lang="python">from __future__ import division |
||
from itertools import islice, count |
from itertools import islice, count |
||
from collections import Counter |
from collections import Counter |
||
Line 3,005: | Line 2,954: | ||
11.0% 5.1% 5.9% |
11.0% 5.1% 5.9% |
||
10.9% 4.6% 6.3%</pre> |
10.9% 4.6% 6.3%</pre> |
||
=={{header|R}}== |
=={{header|R}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="r"> |
||
pbenford <- function(d){ |
pbenford <- function(d){ |
||
return(log10(1+(1/d))) |
return(log10(1+(1/d))) |
||
Line 3,051: | Line 2,999: | ||
8 0.053 0.05115 0.184748 |
8 0.053 0.05115 0.184748 |
||
9 0.045 0.04576 0.075749 |
9 0.045 0.04576 0.075749 |
||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="racket">#lang racket |
||
(define (log10 n) (/ (log n) (log 10))) |
(define (log10 n) (/ (log n) (log 10))) |
||
Line 3,089: | Line 3,036: | ||
;; 8: 5.1% 5.1% |
;; 8: 5.1% 5.1% |
||
;; 9: 4.6% 4.6%</syntaxhighlight> |
;; 9: 4.6% 4.6%</syntaxhighlight> |
||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
(formerly Perl 6) |
(formerly Perl 6) |
||
{{Works with|rakudo|2016-10-24}} |
{{Works with|rakudo|2016-10-24}} |
||
<syntaxhighlight lang=raku line>sub benford(@a) { bag +« @a».substr(0,1) } |
<syntaxhighlight lang="raku line">sub benford(@a) { bag +« @a».substr(0,1) } |
||
sub show(%distribution) { |
sub show(%distribution) { |
||
Line 3,131: | Line 3,077: | ||
8: 5.16% | 5.12% | 0.05% |
8: 5.16% | 5.12% | 0.05% |
||
9: 1.88% | 4.58% | 2.70%</pre> |
9: 1.88% | 4.58% | 2.70%</pre> |
||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
The REXX language (for the most part) hasn't any high math functions, so the '''e''', '''ln''', and '''log''' functions were included herein. |
The REXX language (for the most part) hasn't any high math functions, so the '''e''', '''ln''', and '''log''' functions were included herein. |
||
Line 3,137: | Line 3,082: | ||
For the extra credit stuff, it was chosen to generate Fibonacci and factorials rather than find a |
For the extra credit stuff, it was chosen to generate Fibonacci and factorials rather than find a |
||
web─page with them listed, as each list is very easy to generate. |
web─page with them listed, as each list is very easy to generate. |
||
<syntaxhighlight lang=rexx>/*REXX pgm demonstrates Benford's law applied to 2 common functions (30 dec. digs used).*/ |
<syntaxhighlight lang="rexx">/*REXX pgm demonstrates Benford's law applied to 2 common functions (30 dec. digs used).*/ |
||
numeric digits length( e() ) - length(.) /*width of (e) for LN & LOG precision.*/ |
numeric digits length( e() ) - length(.) /*width of (e) for LN & LOG precision.*/ |
||
parse arg N .; if N=='' | N=="," then N= 1000 /*allow sample size to be specified. */ |
parse arg N .; if N=='' | N=="," then N= 1000 /*allow sample size to be specified. */ |
||
Line 3,222: | Line 3,167: | ||
9 0.0426 0.0457575 |
9 0.0426 0.0457575 |
||
</pre> |
</pre> |
||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
<syntaxhighlight lang=ring> |
<syntaxhighlight lang="ring"> |
||
# Project : Benford's law |
# Project : Benford's law |
||
Line 3,272: | Line 3,216: | ||
9 4.500 4.576 |
9 4.500 4.576 |
||
</pre> |
</pre> |
||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
<syntaxhighlight lang=ruby>EXPECTED = (1..9).map{|d| Math.log10(1+1.0/d)} |
<syntaxhighlight lang="ruby">EXPECTED = (1..9).map{|d| Math.log10(1+1.0/d)} |
||
def fib(n) |
def fib(n) |
||
Line 3,348: | Line 3,291: | ||
9: 10.8% 4.6% 6.2% |
9: 10.8% 4.6% 6.2% |
||
</pre> |
</pre> |
||
=={{header|Run BASIC}}== |
=={{header|Run BASIC}}== |
||
<syntaxhighlight lang=runbasic> |
<syntaxhighlight lang="runbasic"> |
||
N = 1000 |
N = 1000 |
||
for i = 0 to N - 1 |
for i = 0 to N - 1 |
||
Line 3,383: | Line 3,325: | ||
</syntaxhighlight> |
</syntaxhighlight> |
||
<table border=1><TR bgcolor=wheat><TD>Digit<td>Actual<td>Expected</td><tr><tr align=right><td>1</td><td>30.100</td><td>30.103</td></tr><tr align=right><td>2</td><td>17.700</td><td>17.609</td></tr><tr align=right><td>3</td><td>12.500</td><td>12.494</td></tr><tr align=right><td>4</td><td> 9.500</td><td> 9.691</td></tr><tr align=right><td>5</td><td> 8.000</td><td> 7.918</td></tr><tr align=right><td>6</td><td> 6.700</td><td> 6.695</td></tr><tr align=right><td>7</td><td> 5.600</td><td> 5.799</td></tr><tr align=right><td>8</td><td> 5.300</td><td> 5.115</td></tr><tr align=right><td>9</td><td> 4.500</td><td> 4.576</td></tr></table> |
<table border=1><TR bgcolor=wheat><TD>Digit<td>Actual<td>Expected</td><tr><tr align=right><td>1</td><td>30.100</td><td>30.103</td></tr><tr align=right><td>2</td><td>17.700</td><td>17.609</td></tr><tr align=right><td>3</td><td>12.500</td><td>12.494</td></tr><tr align=right><td>4</td><td> 9.500</td><td> 9.691</td></tr><tr align=right><td>5</td><td> 8.000</td><td> 7.918</td></tr><tr align=right><td>6</td><td> 6.700</td><td> 6.695</td></tr><tr align=right><td>7</td><td> 5.600</td><td> 5.799</td></tr><tr align=right><td>8</td><td> 5.300</td><td> 5.115</td></tr><tr align=right><td>9</td><td> 4.500</td><td> 4.576</td></tr></table> |
||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
{{works with|rustc|1.12 stable}} |
{{works with|rustc|1.12 stable}} |
||
Line 3,389: | Line 3,330: | ||
This solution uses the ''num'' create for arbitrary-precision integers and the ''num_traits'' create for the ''zero'' and ''one'' implementations. It computes the Fibonacci numbers from scratch via the ''fib'' function. |
This solution uses the ''num'' create for arbitrary-precision integers and the ''num_traits'' create for the ''zero'' and ''one'' implementations. It computes the Fibonacci numbers from scratch via the ''fib'' function. |
||
<syntaxhighlight lang=rust> |
<syntaxhighlight lang="rust"> |
||
extern crate num_traits; |
extern crate num_traits; |
||
extern crate num; |
extern crate num; |
||
Line 3,455: | Line 3,396: | ||
9: 0.045 v. 0.046 |
9: 0.045 v. 0.046 |
||
</pre> |
</pre> |
||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
<syntaxhighlight lang=scala>// Fibonacci Sequence (begining with 1,1): 1 1 2 3 5 8 13 21 34 55 ... |
<syntaxhighlight lang="scala">// Fibonacci Sequence (begining with 1,1): 1 1 2 3 5 8 13 21 34 55 ... |
||
val fibs : Stream[BigInt] = { def series(i:BigInt,j:BigInt):Stream[BigInt] = i #:: series(j, i+j); series(1,0).tail.tail } |
val fibs : Stream[BigInt] = { def series(i:BigInt,j:BigInt):Stream[BigInt] = i #:: series(j, i+j); series(1,0).tail.tail } |
||
Line 3,503: | Line 3,443: | ||
=={{header|Scheme}}== |
=={{header|Scheme}}== |
||
{{works with|Chez Scheme}} |
{{works with|Chez Scheme}} |
||
<syntaxhighlight lang=scheme>; Compute the probability of leading digit d (an integer [1,9]) according to Benford's law. |
<syntaxhighlight lang="scheme">; Compute the probability of leading digit d (an integer [1,9]) according to Benford's law. |
||
(define benford-probability |
(define benford-probability |
||
Line 3,613: | Line 3,553: | ||
8 0.05115 0.06646 0.01531 |
8 0.05115 0.06646 0.01531 |
||
9 0.04576 0.05831 0.01255</pre> |
9 0.04576 0.05831 0.01255</pre> |
||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
<syntaxhighlight lang=ruby>var (actuals, expected) = ([], []) |
<syntaxhighlight lang="ruby">var (actuals, expected) = ([], []) |
||
var fibonacci = 1000.of {|i| fib(i).digit(0) } |
var fibonacci = 1000.of {|i| fib(i).digit(0) } |
||
Line 3,645: | Line 3,584: | ||
9 : 4.50 % 4.58 % |
9 : 4.50 % 4.58 % |
||
</pre> |
</pre> |
||
=={{header|SQL}}== |
=={{header|SQL}}== |
||
If we load some numbers into a table, we can do the sums without too much difficulty. I tried to make this as database-neutral as possible, but I only had Oracle handy to test it on. |
If we load some numbers into a table, we can do the sums without too much difficulty. I tried to make this as database-neutral as possible, but I only had Oracle handy to test it on. |
||
Line 3,651: | Line 3,589: | ||
The query is the same for any number sequence you care to put in the <tt>benford</tt> table. |
The query is the same for any number sequence you care to put in the <tt>benford</tt> table. |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="sql">-- Create table |
||
create table benford (num integer); |
create table benford (num integer); |
||
Line 3,710: | Line 3,648: | ||
9 rows selected.</pre> |
9 rows selected.</pre> |
||
=={{header|Stata}}== |
=={{header|Stata}}== |
||
<syntaxhighlight lang=stata>clear |
<syntaxhighlight lang="stata">clear |
||
set obs 1000 |
set obs 1000 |
||
scalar phi=(1+sqrt(5))/2 |
scalar phi=(1+sqrt(5))/2 |
||
Line 3,741: | Line 3,678: | ||
Assuming the data are random, one can also do a goodness of fit [https://en.wikipedia.org/wiki/Pearson%27s_chi-squared_test chi-square test]: |
Assuming the data are random, one can also do a goodness of fit [https://en.wikipedia.org/wiki/Pearson%27s_chi-squared_test chi-square test]: |
||
<syntaxhighlight lang=stata>// chi-square statistic |
<syntaxhighlight lang="stata">// chi-square statistic |
||
chisq=sum((f-p):^2:/p) |
chisq=sum((f-p):^2:/p) |
||
chisq |
chisq |
||
Line 3,754: | Line 3,691: | ||
The fit is not as good with the sequence (2+sqrt(2))^n: |
The fit is not as good with the sequence (2+sqrt(2))^n: |
||
<syntaxhighlight lang=stata>clear |
<syntaxhighlight lang="stata">clear |
||
set obs 500 |
set obs 500 |
||
scalar s=2+sqrt(2) |
scalar s=2+sqrt(2) |
||
Line 3,788: | Line 3,725: | ||
Now the p-value is less than the usual 5% risk, and one would reject the hypothesis that the data follow the Benford law. |
Now the p-value is less than the usual 5% risk, and one would reject the hypothesis that the data follow the Benford law. |
||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="swift">import Foundation |
||
/* Reads from a file and returns the content as a String */ |
/* Reads from a file and returns the content as a String */ |
||
Line 3,909: | Line 3,845: | ||
8 5.12 5.30 -0.0018 |
8 5.12 5.30 -0.0018 |
||
9 4.58 4.50 0.0008</pre> |
9 4.58 4.50 0.0008</pre> |
||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
<syntaxhighlight lang=tcl>proc benfordTest {numbers} { |
<syntaxhighlight lang="tcl">proc benfordTest {numbers} { |
||
# Count the leading digits (RE matches first digit in each number, |
# Count the leading digits (RE matches first digit in each number, |
||
# even if negative) |
# even if negative) |
||
Line 3,931: | Line 3,866: | ||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
Demonstrating with Fibonacci numbers: |
Demonstrating with Fibonacci numbers: |
||
<syntaxhighlight lang=tcl>proc fibs n { |
<syntaxhighlight lang="tcl">proc fibs n { |
||
for {set a 1;set b [set i 0]} {$i < $n} {incr i} { |
for {set a 1;set b [set i 0]} {$i < $n} {incr i} { |
||
lappend result [set b [expr {$a + [set a $b]}]] |
lappend result [set b [expr {$a + [set a $b]}]] |
||
Line 3,952: | Line 3,887: | ||
9 | 4.50% | 4.58% |
9 | 4.50% | 4.58% |
||
</pre> |
</pre> |
||
=={{header|VBA (Visual Basic for Application)}}== |
=={{header|VBA (Visual Basic for Application)}}== |
||
<syntaxhighlight lang=vb> |
<syntaxhighlight lang="vb"> |
||
Sub BenfordLaw() |
Sub BenfordLaw() |
||
Line 3,988: | Line 3,921: | ||
} |
} |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|Visual FoxPro}}== |
=={{header|Visual FoxPro}}== |
||
<syntaxhighlight lang=vfp> |
<syntaxhighlight lang="vfp"> |
||
#DEFINE CTAB CHR(9) |
#DEFINE CTAB CHR(9) |
||
#DEFINE COMMA "," |
#DEFINE COMMA "," |
||
Line 4,047: | Line 3,979: | ||
Correlation Coefficient: 0.999908 |
Correlation Coefficient: 0.999908 |
||
</pre> |
</pre> |
||
=={{header|Vlang}}== |
=={{header|Vlang}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
<syntaxhighlight lang=vlang>import math |
<syntaxhighlight lang="vlang">import math |
||
fn fib1000() []f64 { |
fn fib1000() []f64 { |
||
Line 4,091: | Line 4,022: | ||
9 0.045 0.046 |
9 0.045 0.046 |
||
</pre> |
</pre> |
||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
<syntaxhighlight lang=ecmascript>import "/fmt" for Fmt |
<syntaxhighlight lang="ecmascript">import "/fmt" for Fmt |
||
var fib1000 = Fn.new { |
var fib1000 = Fn.new { |
||
Line 4,148: | Line 4,078: | ||
9 0.045 0.046 |
9 0.045 0.046 |
||
</pre> |
</pre> |
||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
<syntaxhighlight lang=zkl>show( // use list (fib(1)...fib(1000)) --> (1..4.34666e+208) |
<syntaxhighlight lang="zkl">show( // use list (fib(1)...fib(1000)) --> (1..4.34666e+208) |
||
(0).pump(1000,List,fcn(ab){ab.append(ab.sum(0.0)).pop(0)}.fp(L(1,1))), |
(0).pump(1000,List,fcn(ab){ab.append(ab.sum(0.0)).pop(0)}.fp(L(1,1))), |
||
"First 1000 Fibonacci numbers"); |
"First 1000 Fibonacci numbers"); |
||
Line 4,167: | Line 4,096: | ||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
{{trans|CoffeeScript}} |
{{trans|CoffeeScript}} |
||
<syntaxhighlight lang=zkl>var BN=Import("zklBigNum"); |
<syntaxhighlight lang="zkl">var BN=Import("zklBigNum"); |
||
fcn fibgen(a,b) { return(a,self.fcn.fp(b,a+b)) } //-->L(fib,fcn) |
fcn fibgen(a,b) { return(a,self.fcn.fp(b,a+b)) } //-->L(fib,fcn) |
||
Line 4,202: | Line 4,131: | ||
9 0.045 0.046 |
9 0.045 0.046 |
||
</pre> |
</pre> |
||
=={{header|ZX Spectrum Basic}}== |
=={{header|ZX Spectrum Basic}}== |
||
{{trans|Liberty BASIC}} |
{{trans|Liberty BASIC}} |
||
<syntaxhighlight lang=zxbasic>10 RANDOMIZE |
<syntaxhighlight lang="zxbasic">10 RANDOMIZE |
||
20 DIM b(9) |
20 DIM b(9) |
||
30 LET n=100 |
30 LET n=100 |