Benford's law: Difference between revisions
Content added Content deleted
m (→Julia) |
No edit summary |
||
Line 1,670: | Line 1,670: | ||
[[File:Fōrmulæ - Benford's law 14.png]] |
[[File:Fōrmulæ - Benford's law 14.png]] |
||
=={{header|FutureBasic}}== |
|||
<syntaxhighlight lang="futurebasic"> |
|||
Short t, i, j, k, m |
|||
Double a(9), z |
|||
Double phi, psi |
|||
CFStringRef s |
|||
print @"Benford:" |
|||
for i = 1 to 9 |
|||
a(i) = log10( 1 + 1 / i ) |
|||
print fn StringWithFormat( @"%.3f ", a(i) ), |
|||
next |
|||
// Fibonacci according to DeMoivre and Binet |
|||
for t = 1 to 9 : a(t) = 0 : next // Clean array |
|||
phi = ( 1 + sqr(5) ) / 2 |
|||
psi = ( 1 - sqr(5) ) / 2 |
|||
for i = 1 to 1000 |
|||
z = ( phi^i - psi^i ) / sqr( 5 ) |
|||
s = fn StringWithFormat( @"%e", z) // Get first digit |
|||
t = fn StringIntegerValue( left( s, 1 ) ) |
|||
a(t) = a(t) + 1 |
|||
next |
|||
print @"\n\nFibonacci:" |
|||
for i = 1 to 9 |
|||
print fn StringWithFormat( @"%.3f ", a(i) / 1000 ), |
|||
next |
|||
// Multiplication tables |
|||
for t = 1 to 9 : a(t) = 0 : next // Clean array |
|||
for i = 1 to 10 |
|||
for j = 1 to 10 |
|||
for k = 1 to 10 |
|||
for m = 1 to 10 |
|||
z = i * j * k * m |
|||
s = fn StringWithFormat( @"%e", z ) |
|||
t = fn StringIntegerValue( left( s, 1 ) ) |
|||
a(t) = a(t) + 1 |
|||
next |
|||
next |
|||
next |
|||
next |
|||
print @"\n\nMultiplication:" |
|||
for i = 1 to 9 |
|||
print fn StringWithFormat( @"%.3f ", a(i) / 1e4 ), |
|||
next |
|||
// Factorials according to DeMoivre and Stirling |
|||
for t = 1 to 9 : a(t) = 0 : next // Clean array |
|||
for i = 10 to 110 |
|||
z = sqr(2 * pi * i ) * (i / exp(1) )^i |
|||
s = fn StringWithFormat( @"%e", z ) |
|||
t = fn StringIntegerValue( left( s, 1 ) ) |
|||
a(t) = a(t) + 1 |
|||
next |
|||
print @"\n\nFactorials:" |
|||
for i = 1 to 9 |
|||
print fn StringWithFormat( @"%.3f ", a(i) / 100 ), |
|||
next |
|||
handleevents |
|||
}</syntaxhighlight> |
|||
{{Out}} |
|||
<pre> |
|||
Benford: |
|||
0.301 0.176 0.125 0.097 0.079 0.067 0.058 0.051 0.046 |
|||
Fibonacci: |
|||
0.301 0.177 0.125 0.096 0.080 0.067 0.056 0.053 0.045 |
|||
Multiplication: |
|||
0.302 0.181 0.124 0.103 0.071 0.061 0.055 0.055 0.047 |
|||
Factorials: |
|||
0.35 0.16 0.12 0.06 0.06 0.06 0.02 0.10 0.08 |
|||
</pre> |
|||
=={{header|Go}}== |
=={{header|Go}}== |