Data update

This commit is contained in:
Ingy döt Net 2026-04-30 12:34:36 -04:00
parent 4bb20c9b71
commit cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions

View file

@ -0,0 +1,23 @@
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Numerics.Float_Random; use Ada.Numerics.Float_Random;
procedure Test_Monte_Carlo is
Dice : Generator;
function Pi (Throws : Positive) return Float is
Inside : Natural := 0;
begin
for Throw in 1..Throws loop
if Random (Dice) ** 2 + Random (Dice) ** 2 <= 1.0 then
Inside := Inside + 1;
end if;
end loop;
return 4.0 * Float (Inside) / Float (Throws);
end Pi;
begin
Put_Line (" 10_000:" & Float'Image (Pi ( 10_000)));
Put_Line (" 100_000:" & Float'Image (Pi ( 100_000)));
Put_Line (" 1_000_000:" & Float'Image (Pi ( 1_000_000)));
Put_Line (" 10_000_000:" & Float'Image (Pi ( 10_000_000)));
Put_Line ("100_000_000:" & Float'Image (Pi (100_000_000)));
end Test_Monte_Carlo;

View file

@ -0,0 +1,11 @@
on montecarlo(n)
set m to 0
repeat n times
set x to random number from 0.0 to 1.0
set y to random number from 0.0 to 1.0
if x ^ 2 + y ^ 2 < 1 then set m to m + 1
end repeat
return 4 * m / n
end montecarlo
montecarlo(1000000)

View file

@ -0,0 +1,21 @@
local fmt = require "fmt"
local function mcpi(n)
local inside = 0
for _ = 1, n do
local x = math.random()
local y = math.random()
if x * x + y * y <= 1 then inside += 1 end
end
return 4 * inside / n
end
print("Iterations -> Approx Pi -> Error%")
print("---------- ---------- ------")
local n = 1000
while n <= 1e8 do
local pi = mcpi(n)
local err = math.abs(math.pi - pi) / math.pi * 100.0
fmt.print("%9d -> %10.8f -> %6.4f", n, pi, err)
n *= 10
end

View file

@ -0,0 +1,17 @@
function Get-Pi ($Iterations = 10000) {
$InCircle = 0
for ($i = 0; $i -lt $Iterations; $i++) {
$x = Get-Random 1.0
$y = Get-Random 1.0
if ([Math]::Sqrt($x * $x + $y * $y) -le 1) {
$InCircle++
}
}
$Pi = [decimal] $InCircle / $Iterations * 4
$RealPi = [decimal] "3.141592653589793238462643383280"
$Diff = [Math]::Abs(($Pi - $RealPi) / $RealPi * 100)
New-Object PSObject `
| Add-Member -PassThru NoteProperty Iterations $Iterations `
| Add-Member -PassThru NoteProperty Pi $Pi `
| Add-Member -PassThru NoteProperty "% Difference" $Diff
}