Initial commit: Pi calculation benchmark with 34 languages
- Added implementations for: bash, brainfuck, c, cpp, crystal, csharp, d, dart, elixir, erlang, fortran, go, haskell, java, javascript, julia, kotlin, objective-c, scala, typescript, lua, nim, odin, perl, php, python, r, ruby, rust, swift, zig, assembly, vimscript, wolfram - All implementations use Machin's formula: π/4 = 4*arctan(1/5) - arctan(1/239) - Build system with ./build.sh, test system with ./test.sh - Performance testing with ./run_all.sh - Comprehensive README.md explaining performance differences - Test framework verifies correctness against known π values
This commit is contained in:
Executable
+29
@@ -0,0 +1,29 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Julia Build Script
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "$0")/.." && pwd)"
|
||||
cd "$SCRIPT_DIR"
|
||||
|
||||
echo "=== Julia Build ==="
|
||||
echo ""
|
||||
|
||||
# Julia är ett interpreterat språk, men vi kan förkompilera för snabbare start
|
||||
# Skapa wrapper script
|
||||
|
||||
mkdir -p bin
|
||||
cat > bin/print_hej << 'EOF'
|
||||
#!/bin/bash
|
||||
SCRIPT_DIR="$(cd "$(dirname "$0")/.." && pwd)"
|
||||
cd "$SCRIPT_DIR"
|
||||
julia src/print_hej.jl "$@"
|
||||
EOF
|
||||
chmod +x bin/print_hej
|
||||
|
||||
echo "✓ Ingen kompilering behövs för Julia"
|
||||
echo " Använder Julia interpreter"
|
||||
echo ""
|
||||
echo "Wrapper script skapad: bin/print_hej"
|
||||
echo ""
|
||||
echo "För att köra:"
|
||||
echo " ./bin/print_hej [decimaler]"
|
||||
Executable
+19
@@ -0,0 +1,19 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Julia Test Script
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "$0")/.." && pwd)"
|
||||
cd "$SCRIPT_DIR"
|
||||
|
||||
echo "=== Julia Test ==="
|
||||
echo ""
|
||||
|
||||
# Testa pi-beräkning med olika antal decimaler
|
||||
for decimals in 1 2 5 10 100; do
|
||||
result=$(./bin/print_hej $decimals 2>/dev/null)
|
||||
if [ $? -eq 0 ]; then
|
||||
echo "✓ $decimals decimaler: $result"
|
||||
else
|
||||
echo "✗ $decimals decimaler misslyckades"
|
||||
fi
|
||||
done
|
||||
@@ -0,0 +1,65 @@
|
||||
#!/usr/bin/env julia
|
||||
|
||||
# Pi calculation using Machin's formula
|
||||
# pi/4 = 4*arctan(1/5) - arctan(1/239)
|
||||
|
||||
# Calculate pi using Machin's formula
|
||||
function calculate_pi(decimals::Int)
|
||||
if decimals < 1
|
||||
decimals = 100
|
||||
end
|
||||
|
||||
# Set precision (need extra bits for accuracy)
|
||||
setprecision(decimals * 4 + 100)
|
||||
|
||||
# Machin's formula: pi/4 = 4*arctan(1/5) - arctan(1/239)
|
||||
pi_val = BigFloat(4) * atan(BigFloat(1)/5) - atan(BigFloat(1)/239)
|
||||
pi_val *= 4
|
||||
|
||||
# Format output
|
||||
pi_str = string(pi_val)
|
||||
|
||||
# Extract just the digits we need
|
||||
if decimals == 0
|
||||
return "3"
|
||||
else
|
||||
# Find decimal point
|
||||
decimal_pos = findfirst('.', pi_str)
|
||||
if decimal_pos === nothing
|
||||
return pi_str
|
||||
end
|
||||
|
||||
# Get digits after decimal point
|
||||
after_decimal = pi_str[decimal_pos+1:end]
|
||||
|
||||
# Pad or truncate to desired length
|
||||
if length(after_decimal) < decimals
|
||||
after_decimal *= repeat("0", decimals - length(after_decimal))
|
||||
else
|
||||
after_decimal = after_decimal[1:decimals]
|
||||
end
|
||||
|
||||
return "3." * after_decimal
|
||||
end
|
||||
end
|
||||
|
||||
# Main
|
||||
function main()
|
||||
# Get decimals from command line
|
||||
decimals = 100
|
||||
if length(ARGS) > 0
|
||||
try
|
||||
decimals = parse(Int, ARGS[1])
|
||||
if decimals < 1
|
||||
decimals = 100
|
||||
end
|
||||
catch
|
||||
decimals = 100
|
||||
end
|
||||
end
|
||||
|
||||
result = calculate_pi(decimals)
|
||||
println(result)
|
||||
end
|
||||
|
||||
main()
|
||||
Reference in New Issue
Block a user