Benford's law: Difference between revisions

m
Automated syntax highlighting fixup (second round - minor fixes)
m (syntax highlighting fixup automation)
m (Automated syntax highlighting fixup (second round - minor fixes))
Line 31:
* A starting page on Wolfram Mathworld is {{Wolfram|Benfords|Law}}.
<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}}==
<syntaxhighlight lang="8th">
: n:log10e ` 1 10 ln / ` ;
 
Line 157 ⟶ 115:
9 0.0458 0.045
</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}}==
 
The program reads the Fibonacci-Numbers from the standard input. Each input line is supposed to hold N, followed by Fib(N).
 
<syntaxhighlight lang=Ada"ada">with Ada.Text_IO, Ada.Numerics.Generic_Elementary_Functions;
 
procedure Benford is
Line 230 ⟶ 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:
 
<syntaxhighlight lang=Ada"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.
Line 249 ⟶ 246:
8 1003 490.7 10.46 5.12 5.34
9 1006 438.9 10.49 4.58 5.91</pre>
 
=={{header|Aime}}==
<syntaxhighlight lang="aime">text
sum(text a, text b)
{
Line 350 ⟶ 346:
8 5.115 5.300
9 4.575 4.5 </pre>
 
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
Uses Algol 68G's LONG LONG INT which has programmer specifiable precision.
<syntaxhighlight lang="algol68">BEGIN
# set the number of digits for LONG LONG INT values #
PR precision 256 PR
Line 403 ⟶ 398:
Benford: 0.046 actual: 0.045
</pre>
 
=={{header|AutoHotkey}}==
{{works with|AutoHotkey_L}}(AutoHotkey1.1+)
<syntaxhighlight lang=AutoHotkey"autohotkey">SetBatchLines, -1
fib := NStepSequence(1, 1, 2, 1000)
Out := "Digit`tExpected`tObserved`tDeviation`n"
Line 460 ⟶ 454:
8 5.115252 5.300000 0.184748
9 4.575749 4.500000 0.075749</pre>
 
=={{header|AWK}}==
<syntaxhighlight lang=AWK"awk">
# syntax: GAWK -f BENFORDS_LAW.AWK
BEGIN {
Line 504 ⟶ 497:
9 4.5757 4.5000 0.0757
</pre>
 
=={{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)
<syntaxhighlight lang=BCPL"bcpl">
GET "libhdr"
 
Line 566 ⟶ 558:
9 0.045 0.046
</pre>
 
=={{header|C}}==
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <math.h>
Line 646 ⟶ 637:
8 0.053 0.051
9 0.045 0.046</pre>
 
=={{header|C++}}==
<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"
#include <cln/integer.h>
Line 723 ⟶ 713:
9 : 4.5 % 4.58 %
</pre>
 
=={{header|Clojure}}==
<syntaxhighlight lang="lisp">(ns example
(:gen-class))
 
Line 836 ⟶ 825:
9 5.76 4.58 1.18
</pre>
 
=={{header|CoffeeScript}}==
<syntaxhighlight lang="coffeescript">fibgen = () ->
a = 1; b = 0
return () ->
Line 871 ⟶ 859:
8 0.053 0.051
9 0.045 0.046</pre>
 
=={{header|Common Lisp}}==
<syntaxhighlight lang="lisp">(defun calculate-distribution (numbers)
"Return the frequency distribution of the most significant nonzero
digits in the given list of numbers. The first element of the list
Line 929 ⟶ 916:
8 0.053 0.051
9 0.045 0.046</pre>
 
=={{header|Crystal}}==
{{trans|Ruby}}
<syntaxhighlight lang="ruby">require "big"
 
EXPECTED = (1..9).map{ |d| Math.log10(1 + 1.0 / d) }
Line 1,007 ⟶ 993:
9: 12.3% 4.6% 7.7%
</pre>
 
=={{header|D}}==
{{trans|Scala}}
<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) {
Line 1,049 ⟶ 1,034:
===Alternative Version===
The output is the same.
<syntaxhighlight lang="d">import std.stdio, std.range, std.math, std.conv, std.bigint,
std.algorithm, std.array;
 
Line 1,070 ⟶ 1,055:
See [https://rosettacode.org/wiki/Benford%27s_law#Pascal Pascal].
=={{header|Elixir}}==
<syntaxhighlight lang="elixir">defmodule Benfords_law do
def distribution(n), do: :math.log10( 1 + (1 / n) )
Line 1,102 ⟶ 1,087:
9 0.045 0.04575749056067514
</pre>
 
=={{header|Erlang}}==
<syntaxhighlight lang=Erlang"erlang">
-module( benfords_law ).
-export( [actual_distribution/1, distribution/1, task/0] ).
Line 1,145 ⟶ 1,129:
9 0.045 0.04575749056067514
</pre>
 
=={{header|F sharp|F#}}==
 
For Fibonacci code, see https://rosettacode.org/wiki/Fibonacci_sequence#F.89
 
<syntaxhighlight lang="fsharp">open System
 
let fibonacci = Seq.unfold (fun (x, y) -> Some(x, (y, x + y))) (0I,1I)
Line 1,180 ⟶ 1,163:
 
</pre>
 
=={{header|Factor}}==
<syntaxhighlight lang="factor">USING: assocs compiler.tree.propagation.call-effect formatting
kernel math math.functions math.statistics math.text.utils
sequences ;
Line 1,224 ⟶ 1,206:
9 0.0458 0.0450
</pre>
 
=={{header|Forth}}==
<syntaxhighlight lang="forth">: 3drop drop 2drop ;
: f2drop fdrop fdrop ;
 
Line 1,286 ⟶ 1,267:
8 0.053 0.0512
9 0.045 0.0458 ok</pre>
 
=={{header|Fortran}}==
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/" -*-
Compilation started at Sat May 18 01:13:00
 
Line 1,299 ⟶ 1,279:
Compilation finished at Sat May 18 01:13:00</syntaxhighlight>
 
<syntaxhighlight lang="fortran">subroutine fibber(a,b,c,d)
! compute most significant digits, Fibonacci like.
implicit none
Line 1,368 ⟶ 1,348:
write(6,*) (count(i)/1000.0 ,i=1,9),'LEADING FIBONACCI DIGIT'
end program benford</syntaxhighlight>
 
=={{header|FreeBASIC}}==
{{libheader|GMP}}
<syntaxhighlight lang="freebasic">' version 27-10-2016
' compile with: fbc -s console
 
Line 1,488 ⟶ 1,467:
8 71038 7.10 % 5.12 % -1.989 %
9 70320 7.03 % 4.58 % -2.456 %</pre>
 
=={{header|Fōrmulæ}}==
 
Line 1,496 ⟶ 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.
 
=={{header|Go}}==
<syntaxhighlight lang="go">package main
 
import (
Line 1,543 ⟶ 1,520:
9 0.045 0.046
</pre>
 
=={{header|Groovy}}==
'''Solution:'''<br>
Uses [[Fibonacci_sequence#Analytic_8|Fibonacci sequence analytic formula]]
{{trans|Java}}
<syntaxhighlight lang="groovy">def tallyFirstDigits = { size, generator ->
def population = (0..<size).collect { generator(it) }
def firstDigits = [0]*10
Line 1,558 ⟶ 1,534:
 
'''Test:'''
<syntaxhighlight lang="groovy">def digitCounts = tallyFirstDigits(1000, aFib)
println "d actual predicted"
(1..<10).each {
Line 1,575 ⟶ 1,551:
8 0.053000 0.051153
9 0.045000 0.045757</pre>
 
=={{header|Haskell}}==
<syntaxhighlight lang="haskell">import qualified Data.Map as M
import Data.Char (digitToInt)
 
Line 1,610 ⟶ 1,585:
(9,0.045,0.0457574905606751)]
</pre>
 
=={{header|Icon}} and {{header|Unicon}}==
 
The following solution works in both languages.
 
<syntaxhighlight lang="unicon">global counts, total
 
procedure main()
Line 1,665 ⟶ 1,639:
->
</pre>
 
=={{header|J}}==
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"j">log10 =: 10&^.
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
Line 1,704 ⟶ 1,677:
 
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"java">import java.math.BigInteger;
import java.util.Locale;
 
Line 1,746 ⟶ 1,718:
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.
 
=={{header|JavaScript}}==
<syntaxhighlight lang="javascript">const fibseries = n => [...Array(n)]
.reduce(
(fib, _, i) => i < 2 ? (
Line 1,777 ⟶ 1,748:
7: (3) [8, 0.053, 0.05115252244738129]
8: (3) [9, 0.045, 0.04575749056067514]</pre>
 
=={{header|jq}}==
{{works with|jq|1.4}}
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, ...
# Numerical accuracy is insufficient beyond about 1450.
def fibonacci(n):
Line 1,959 ⟶ 1,929:
 
χ² = 3204.8072</pre>
 
=={{header|Julia}}==
 
<syntaxhighlight lang=Julia"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)
Line 1,980 ⟶ 1,949:
9 0.045 0.0457575
</pre>
 
=={{header|Kotlin}}==
<syntaxhighlight lang="scala">import java.math.BigInteger
 
interface NumberGenerator {
Line 2,021 ⟶ 1,989:
 
fun main(a: Array<String>) = println(Benford(FibonacciGenerator))</syntaxhighlight>
 
=={{header|Liberty BASIC}}==
Using function from
http://rosettacode.org/wiki/Fibonacci_sequence#Liberty_BASIC
<syntaxhighlight lang="lb">
dim bin(9)
 
Line 2,076 ⟶ 2,043:
9 0.045 0.046
</pre>
 
=={{header|Lua}}==
<syntaxhighlight lang="lua">actual = {}
expected = {}
for i = 1, 9 do
Line 2,109 ⟶ 2,075:
8 0.053 0.051152522447381
9 0.045 0.045757490560675</pre>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">fibdata = Array[First@IntegerDigits@Fibonacci@# &, 1000];
Table[{d, N@Count[fibdata, d]/Length@fibdata, Log10[1. + 1/d]}, {d, 1,
9}] // Grid</syntaxhighlight>
Line 2,124 ⟶ 2,089:
8 0.053 0.0511525
9 0.045 0.0457575</pre>
 
=={{header|NetRexx}}==
<syntaxhighlight lang=NetRexx"netrexx">/* NetRexx */
options replace format comments java crossref symbols nobinary
 
Line 2,182 ⟶ 2,146:
9: 4.500000% 4.575749% 0.075749%
</pre>
 
=={{header|Nim}}==
<syntaxhighlight lang=Nim"nim">import math
import strformat
 
Line 2,276 ⟶ 2,239:
8 0.0530 0.0512
9 0.0450 0.0458</pre>
 
=={{header|Oberon-2}}==
{{Works with|oo2c version 2}}
<syntaxhighlight lang="oberon2">
MODULE BenfordLaw;
IMPORT
Line 2,338 ⟶ 2,300:
9 0.045 0.046
</pre>
 
=={{header|OCaml}}==
For the Fibonacci sequence, we use the function from
https://rosettacode.org/wiki/Fibonacci_sequence#Arbitrary_Precision<br>
Note the remark about the compilation of the program there.
<syntaxhighlight lang="ocaml">
open Num
 
Line 2,382 ⟶ 2,343:
0.301030 0.176091 0.124939 0.096910 0.079181 0.066947 0.057992 0.051153 0.045757
</pre>
 
=={{header|PARI/GP}}==
<syntaxhighlight lang="parigp">distribution(v)={
my(t=vector(9,n,sum(i=1,#v,v[i]==n)));
print("Digit\tActual\tExpected");
Line 2,415 ⟶ 2,375:
8 51 51
9 46 46</pre>
 
=={{header|Pascal}}==
<syntaxhighlight lang="pascal">program fibFirstdigit;
{$IFDEF FPC}{$MODE Delphi}{$ELSE}{$APPTYPE CONSOLE}{$ENDIF}
uses
Line 2,481 ⟶ 2,440:
8 53 51 -3.92157 %
9 45 45 0.00000 %</pre>
 
=={{header|Perl}}==
<syntaxhighlight lang=Perl"perl">#!/usr/bin/perl
use strict ;
use warnings ;
Line 2,521 ⟶ 2,479:
9 : 4.50 % 4.58 %
</pre>
 
=={{header|Phix}}==
{{trans|Go}}
<!--<syntaxhighlight lang=Phix"phix">(phixonline)-->
<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>
Line 2,569 ⟶ 2,526:
9 4.500 4.576 9 10.060 4.576 9 4.600 4.576
</pre>
 
=={{header|Picat}}==
<syntaxhighlight lang=Picat"picat">go =>
 
N = 1000,
Line 2,630 ⟶ 2,586:
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>
 
=={{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.
<syntaxhighlight lang=PicoLisp"picolisp">
(scl 4)
(load "@lib/misc.l")
Line 2,685 ⟶ 2,640:
9 0.045 0.046
</pre>
 
=={{header|PL/I}}==
<syntaxhighlight lang=PL"pl/Ii">
(fofl, size, subrg):
Benford: procedure options(main); /* 20 October 2013 */
Line 2,741 ⟶ 2,695:
9 0.04575749 0.04499817
</pre>
 
=={{header|PL/pgSQL}}==
<syntaxhighlight lang=SQL"sql">
WITH recursive
constant(val) AS
Line 2,779 ⟶ 2,732:
from benford) c
</syntaxhighlight>
 
=={{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.
<syntaxhighlight lang=PowerShell"powershell">
$url = "https://oeis.org/A000045/b000045.txt"
$file = "$env:TEMP\FibonacciNumbers.txt"
Line 2,814 ⟶ 2,766:
9 45 0.045 0.04576
</pre>
 
=={{header|Prolog}}==
{{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
<syntaxhighlight lang=Prolog"prolog">%_________________________________________________________________
% Does the Fibonacci sequence follow Benford's law?
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Line 2,874 ⟶ 2,825:
5.12% - 5.30%
4.58% - 4.50%</pre>
 
=={{header|PureBasic}}==
<syntaxhighlight lang="purebasic">#MAX_N=1000
NewMap d1.i()
Dim fi.s(#MAX_N)
Line 2,930 ⟶ 2,880:
 
Press Enter...</pre>
 
=={{header|Python}}==
Works with Python 3.X & 2.7
<syntaxhighlight lang="python">from __future__ import division
from itertools import islice, count
from collections import Counter
Line 3,005 ⟶ 2,954:
11.0% 5.1% 5.9%
10.9% 4.6% 6.3%</pre>
 
=={{header|R}}==
<syntaxhighlight lang=R"r">
pbenford <- function(d){
return(log10(1+(1/d)))
Line 3,051 ⟶ 2,999:
8 0.053 0.05115 0.184748
9 0.045 0.04576 0.075749
 
=={{header|Racket}}==
<syntaxhighlight lang=Racket"racket">#lang racket
 
(define (log10 n) (/ (log n) (log 10)))
Line 3,089 ⟶ 3,036:
;; 8: 5.1% 5.1%
;; 9: 4.6% 4.6%</syntaxhighlight>
 
=={{header|Raku}}==
(formerly Perl 6)
{{Works with|rakudo|2016-10-24}}
<syntaxhighlight lang="raku line">sub benford(@a) { bag +« @a».substr(0,1) }
 
sub show(%distribution) {
Line 3,131 ⟶ 3,077:
8: 5.16% | 5.12% | 0.05%
9: 1.88% | 4.58% | 2.70%</pre>
 
=={{header|REXX}}==
The REXX language (for the most part) hasn't any high math functions, so the &nbsp; '''e''', &nbsp; '''ln''', &nbsp; and '''log''' &nbsp; functions were included herein.
Line 3,137 ⟶ 3,082:
For the extra credit stuff, it was chosen to generate Fibonacci and factorials rather than find a
web─page with them listed, &nbsp; 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).*/
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. */
Line 3,222 ⟶ 3,167:
9 0.0426 0.0457575
</pre>
 
=={{header|Ring}}==
<syntaxhighlight lang="ring">
# Project : Benford's law
 
Line 3,272 ⟶ 3,216:
9 4.500 4.576
</pre>
 
=={{header|Ruby}}==
{{trans|Python}}
<syntaxhighlight lang="ruby">EXPECTED = (1..9).map{|d| Math.log10(1+1.0/d)}
 
def fib(n)
Line 3,348 ⟶ 3,291:
9: 10.8% 4.6% 6.2%
</pre>
 
=={{header|Run BASIC}}==
<syntaxhighlight lang="runbasic">
N = 1000
for i = 0 to N - 1
Line 3,383 ⟶ 3,325:
</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>
 
=={{header|Rust}}==
{{works with|rustc|1.12 stable}}
Line 3,389 ⟶ 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.
 
<syntaxhighlight lang="rust">
extern crate num_traits;
extern crate num;
Line 3,455 ⟶ 3,396:
9: 0.045 v. 0.046
</pre>
 
=={{header|Scala}}==
<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 }
 
Line 3,503 ⟶ 3,443:
=={{header|Scheme}}==
{{works with|Chez Scheme}}
<syntaxhighlight lang="scheme">; Compute the probability of leading digit d (an integer [1,9]) according to Benford's law.
 
(define benford-probability
Line 3,613 ⟶ 3,553:
8 0.05115 0.06646 0.01531
9 0.04576 0.05831 0.01255</pre>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">var (actuals, expected) = ([], [])
var fibonacci = 1000.of {|i| fib(i).digit(0) }
 
Line 3,645 ⟶ 3,584:
9 : 4.50 % 4.58 %
</pre>
 
=={{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.
Line 3,651 ⟶ 3,589:
The query is the same for any number sequence you care to put in the <tt>benford</tt> table.
 
<syntaxhighlight lang=SQL"sql">-- Create table
create table benford (num integer);
 
Line 3,710 ⟶ 3,648:
 
9 rows selected.</pre>
 
=={{header|Stata}}==
 
<syntaxhighlight lang="stata">clear
set obs 1000
scalar phi=(1+sqrt(5))/2
Line 3,741 ⟶ 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]:
 
<syntaxhighlight lang="stata">// chi-square statistic
chisq=sum((f-p):^2:/p)
chisq
Line 3,754 ⟶ 3,691:
The fit is not as good with the sequence (2+sqrt(2))^n:
 
<syntaxhighlight lang="stata">clear
set obs 500
scalar s=2+sqrt(2)
Line 3,788 ⟶ 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.
 
=={{header|Swift}}==
 
<syntaxhighlight lang=Swift"swift">import Foundation
 
/* Reads from a file and returns the content as a String */
Line 3,909 ⟶ 3,845:
8 5.12 5.30 -0.0018
9 4.58 4.50 0.0008</pre>
 
=={{header|Tcl}}==
<syntaxhighlight lang="tcl">proc benfordTest {numbers} {
# Count the leading digits (RE matches first digit in each number,
# even if negative)
Line 3,931 ⟶ 3,866:
}</syntaxhighlight>
Demonstrating with Fibonacci numbers:
<syntaxhighlight lang="tcl">proc fibs n {
for {set a 1;set b [set i 0]} {$i < $n} {incr i} {
lappend result [set b [expr {$a + [set a $b]}]]
Line 3,952 ⟶ 3,887:
9 | 4.50% | 4.58%
</pre>
 
 
=={{header|VBA (Visual Basic for Application)}}==
<syntaxhighlight lang="vb">
Sub BenfordLaw()
 
Line 3,988 ⟶ 3,921:
}
</syntaxhighlight>
 
=={{header|Visual FoxPro}}==
<syntaxhighlight lang="vfp">
#DEFINE CTAB CHR(9)
#DEFINE COMMA ","
Line 4,047 ⟶ 3,979:
Correlation Coefficient: 0.999908
</pre>
 
=={{header|Vlang}}==
{{trans|Go}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="vlang">import math
 
fn fib1000() []f64 {
Line 4,091 ⟶ 4,022:
9 0.045 0.046
</pre>
 
=={{header|Wren}}==
{{trans|Go}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascript">import "/fmt" for Fmt
 
var fib1000 = Fn.new {
Line 4,148 ⟶ 4,078:
9 0.045 0.046
</pre>
 
=={{header|zkl}}==
{{trans|Go}}
<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))),
"First 1000 Fibonacci numbers");
Line 4,167 ⟶ 4,096:
}</syntaxhighlight>
{{trans|CoffeeScript}}
<syntaxhighlight lang="zkl">var BN=Import("zklBigNum");
 
fcn fibgen(a,b) { return(a,self.fcn.fp(b,a+b)) } //-->L(fib,fcn)
Line 4,202 ⟶ 4,131:
9 0.045 0.046
</pre>
 
=={{header|ZX Spectrum Basic}}==
{{trans|Liberty BASIC}}
<syntaxhighlight lang="zxbasic">10 RANDOMIZE
20 DIM b(9)
30 LET n=100
10,327

edits