Data update
This commit is contained in:
parent
5150844a7d
commit
4bb20c9b71
7735 changed files with 38060 additions and 199180 deletions
|
|
@ -1,53 +0,0 @@
|
|||
with Ada.Text_IO; use Ada.Text_IO;
|
||||
with Ada.Numerics.Generic_Elementary_Functions;
|
||||
with Ada.Numerics.Discrete_Random;
|
||||
procedure Avglen is
|
||||
package IIO is new Ada.Text_IO.Integer_IO (Positive); use IIO;
|
||||
package LFIO is new Ada.Text_IO.Float_IO (Long_Float); use LFIO;
|
||||
subtype FactN is Natural range 0..20;
|
||||
TESTS : constant Natural := 1_000_000;
|
||||
|
||||
function Factorial (N : FactN) return Long_Float is
|
||||
Result : Long_Float := 1.0;
|
||||
begin
|
||||
for I in 2..N loop Result := Result * Long_Float(I); end loop;
|
||||
return Result;
|
||||
end Factorial;
|
||||
|
||||
function Analytical (N : FactN) return Long_Float is
|
||||
Sum : Long_Float := 0.0;
|
||||
begin
|
||||
for I in 1..N loop
|
||||
Sum := Sum + Factorial(N) / Factorial(N - I) / Long_Float(N)**I;
|
||||
end loop;
|
||||
return Sum;
|
||||
end Analytical;
|
||||
|
||||
function Experimental (N : FactN) return Long_Float is
|
||||
subtype RandInt is Natural range 1..N;
|
||||
package Random is new Ada.Numerics.Discrete_Random(RandInt);
|
||||
seed : Random.Generator;
|
||||
Num : RandInt;
|
||||
count : Natural := 0;
|
||||
bits : array(RandInt'Range) of Boolean;
|
||||
begin
|
||||
Random.Reset(seed);
|
||||
for run in 1..TESTS loop
|
||||
bits := (others => false);
|
||||
for I in RandInt'Range loop
|
||||
Num := Random.Random(seed); exit when bits(Num);
|
||||
bits(Num) := True; count := count + 1;
|
||||
end loop;
|
||||
end loop;
|
||||
return Long_Float(count)/Long_Float(TESTS);
|
||||
end Experimental;
|
||||
|
||||
A, E, err : Long_Float;
|
||||
begin
|
||||
Put_Line(" N avg calc %diff");
|
||||
for I in 1..20 loop
|
||||
A := Analytical(I); E := Experimental(I); err := abs(E-A)/A*100.0;
|
||||
Put(I, Width=>2); Put(E ,Aft=>4, exp=>0); Put(A, Aft=>4, exp=>0);
|
||||
Put(err, Fore=>3, Aft=>3, exp=>0); New_line;
|
||||
end loop;
|
||||
end Avglen;
|
||||
|
|
@ -1,9 +1,11 @@
|
|||
func average n reps .
|
||||
fastfunc average n reps .
|
||||
len f[] n
|
||||
len seen[] n
|
||||
for r to reps
|
||||
f[] = [ ]
|
||||
for i to n : f[] &= random n
|
||||
seen[] = [ ]
|
||||
len seen[] n
|
||||
for i = 1 to n
|
||||
f[i] = random n
|
||||
seen[i] = 0
|
||||
.
|
||||
x = 1
|
||||
while seen[x] = 0
|
||||
seen[x] = 1
|
||||
|
|
@ -13,7 +15,7 @@ func average n reps .
|
|||
.
|
||||
return count / reps
|
||||
.
|
||||
func analytical n .
|
||||
fastfunc analytical n .
|
||||
s = 1
|
||||
t = 1
|
||||
for i = n - 1 downto 1
|
||||
|
|
|
|||
|
|
@ -1,35 +1,31 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #008080;">constant</span> <span style="color: #000000;">MAX</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">20</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #000000;">ITER</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1000000</span>
|
||||
with javascript_semantics
|
||||
constant MAX = 20,
|
||||
ITER = 1000000
|
||||
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">expected</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">atom</span> <span style="color: #7060A8;">sum</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #7060A8;">sum</span> <span style="color: #0000FF;">+=</span> <span style="color: #7060A8;">factorial</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">/</span> <span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">/</span> <span style="color: #7060A8;">factorial</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">-</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #7060A8;">sum</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
function expected(integer n)
|
||||
atom res = 0
|
||||
for i=1 to n do
|
||||
res += factorial(n)/power(n,i)/factorial(n-i)
|
||||
end for
|
||||
return res
|
||||
end function
|
||||
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">count</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">bits</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">ITER</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000000;">x</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
|
||||
<span style="color: #000000;">bits</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
|
||||
<span style="color: #008080;">while</span> <span style="color: #008080;">not</span> <span style="color: #7060A8;">and_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">bits</span><span style="color: #0000FF;">,</span><span style="color: #000000;">x</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000000;">count</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
|
||||
<span style="color: #000000;">bits</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">or_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">bits</span><span style="color: #0000FF;">,</span><span style="color: #000000;">x</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">x</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">rand</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">count</span><span style="color: #0000FF;">/</span><span style="color: #000000;">ITER</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
function test(integer n)
|
||||
integer count = 0
|
||||
for i=1 to ITER do
|
||||
integer x = 1, bits = 0
|
||||
while not and_bits(bits,x) do
|
||||
count += 1
|
||||
bits = or_bits(bits,x)
|
||||
x = power(2,rand(n)-1)
|
||||
end while
|
||||
end for
|
||||
return count/ITER
|
||||
end function
|
||||
|
||||
<span style="color: #004080;">atom</span> <span style="color: #000000;">av</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ex</span>
|
||||
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" n avg. exp. (error%)\n"</span><span style="color: #0000FF;">);</span>
|
||||
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"== ====== ====== ========\n"</span><span style="color: #0000FF;">);</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">MAX</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000000;">av</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">ex</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">expected</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%2d %8.4f %8.4f (%5.3f%%)\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">av</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ex</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">abs</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">-</span><span style="color: #000000;">av</span><span style="color: #0000FF;">/</span><span style="color: #000000;">ex</span><span style="color: #0000FF;">)*</span><span style="color: #000000;">100</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<!--
|
||||
puts(1," n avg. exp. (error%)\n");
|
||||
puts(1,"== ====== ====== ========\n");
|
||||
for n=1 to MAX do
|
||||
atom av = test(n), ex = expected(n)
|
||||
printf(1,"%2d %8.4f %8.4f (%5.3f%%)\n", {n,av,ex,abs(1-av/ex)*100})
|
||||
end for
|
||||
|
|
|
|||
|
|
@ -1,59 +0,0 @@
|
|||
function Get-AnalyticalLoopAverage ( [int]$N )
|
||||
{
|
||||
# Expected loop average = sum from i = 1 to N of N! / (N-i)! / N^(N-i+1)
|
||||
# Equivalently, Expected loop average = sum from i = 1 to N of F(i)
|
||||
# where F(N) = 1, and F(i) = F(i+1)*i/N
|
||||
|
||||
$LoopAverage = $Fi = 1
|
||||
|
||||
If ( $N -eq 1 ) { return $LoopAverage }
|
||||
|
||||
ForEach ( $i in ($N-1)..1 )
|
||||
{
|
||||
$Fi *= $i / $N
|
||||
$LoopAverage += $Fi
|
||||
}
|
||||
return $LoopAverage
|
||||
}
|
||||
|
||||
function Get-ExperimentalLoopAverage ( [int]$N, [int]$Tests = 100000 )
|
||||
{
|
||||
If ( $N -eq 1 ) { return 1 }
|
||||
|
||||
# Using 0 through N-1 instead of 1 through N for speed and simplicity
|
||||
$NMO = $N - 1
|
||||
|
||||
# Create array to hold mapping function
|
||||
$F = New-Object int[] ( $N )
|
||||
|
||||
$Count = 0
|
||||
$Random = New-Object System.Random
|
||||
|
||||
ForEach ( $Test in 1..$Tests )
|
||||
{
|
||||
# Map each number to a random number
|
||||
ForEach ( $i in 0..$NMO )
|
||||
{
|
||||
$F[$i] = $Random.Next( $N )
|
||||
}
|
||||
|
||||
# For each number...
|
||||
ForEach ( $i in 0..$NMO )
|
||||
{
|
||||
# Add the number to the list
|
||||
$List = @()
|
||||
$Count++
|
||||
$List += $X = $i
|
||||
|
||||
# If loop does not yet exist in list...
|
||||
While ( $F[$X] -notin $List )
|
||||
{
|
||||
# Go to the next mapped number and add it to the list
|
||||
$Count++
|
||||
$List += $X = $F[$X]
|
||||
}
|
||||
}
|
||||
}
|
||||
$LoopAvereage = $Count / $N / $Tests
|
||||
return $LoopAvereage
|
||||
}
|
||||
|
|
@ -1,12 +0,0 @@
|
|||
# Display results for N = 1 through 20
|
||||
ForEach ( $N in 1..20 )
|
||||
{
|
||||
$AnalyticalAverage = Get-AnalyticalLoopAverage $N
|
||||
$ExperimentalAverage = Get-ExperimentalLoopAverage $N
|
||||
[pscustomobject] @{
|
||||
N = $N.ToString().PadLeft( 2, ' ' )
|
||||
Analytical = $AnalyticalAverage.ToString( '0.00000000' )
|
||||
Experimental = $ExperimentalAverage.ToString( '0.00000000' )
|
||||
'Error (%)' = ( [math]::Abs( $AnalyticalAverage - $ExperimentalAverage ) / $AnalyticalAverage * 100 ).ToString( '0.00000000' )
|
||||
}
|
||||
}
|
||||
|
|
@ -1,48 +0,0 @@
|
|||
Const MAX = 20
|
||||
Const ITER = 100000
|
||||
|
||||
Function expected(n)
|
||||
Dim sum
|
||||
ni=n
|
||||
For i = 1 To n
|
||||
sum = sum + fact(n) / ni / fact(n-i)
|
||||
ni=ni*n
|
||||
Next
|
||||
expected = sum
|
||||
End Function
|
||||
|
||||
Function test(n )
|
||||
Dim coun,x,bits
|
||||
For i = 1 To ITER
|
||||
x = 1
|
||||
bits = 0
|
||||
Do While Not bits And x
|
||||
count = count + 1
|
||||
bits = bits Or x
|
||||
x =shift(Int(n * Rnd()))
|
||||
Loop
|
||||
Next
|
||||
test = count / ITER
|
||||
End Function
|
||||
|
||||
'VBScript formats numbers but does'nt align them!
|
||||
function rf(v,n,s) rf=right(string(n,s)& v,n):end function
|
||||
|
||||
'some precalculations to speed things up...
|
||||
dim fact(20),shift(20)
|
||||
fact(0)=1:shift(0)=1
|
||||
for i=1 to 20
|
||||
fact(i)=i*fact(i-1)
|
||||
shift(i)=2*shift(i-1)
|
||||
next
|
||||
|
||||
Dim n
|
||||
Wscript.echo "For " & ITER &" iterations"
|
||||
Wscript.Echo " n avg. exp. (error%)"
|
||||
Wscript.Echo "== ====== ====== =========="
|
||||
For n = 1 To MAX
|
||||
av = test(n)
|
||||
ex = expected(n)
|
||||
Wscript.Echo rf(n,2," ")& " "& rf(formatnumber(av, 4),7," ") & " "& _
|
||||
rf(formatnumber(ex,4),6," ")& " ("& rf(Formatpercent(1 - av / ex,4),8," ") & ")"
|
||||
Next
|
||||
Loading…
Add table
Add a link
Reference in a new issue