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:
Ein Anderssono
2026-04-23 00:26:18 +02:00
commit 54d2fecee0
182 changed files with 17471 additions and 0 deletions
+88
View File
@@ -0,0 +1,88 @@
#!/usr/bin/env python3
"""
Beräknar pi med angivet antal decimaler
Användning: python3 print_hej.py <antal_decimaler>
"""
import sys
from decimal import Decimal, getcontext
def arctan(x, precision):
"""
Beräknar arctan(1/x) med Taylor-serien
arctan(1/x) = 1/x - 1/(3*x^3) + 1/(5*x^5) - ...
"""
result = Decimal(0)
x = Decimal(x)
x_squared = x * x
term = Decimal(1) / x
sign = 1
for n in range(1000000):
divisor = Decimal(2 * n + 1)
contrib = term / divisor
if sign > 0:
result += contrib
else:
result -= contrib
term = term / x_squared
sign = -sign
# Avbryt om termen är tillräckligt liten
if term < Decimal(10) ** (-precision - 10):
break
return result
def calculate_pi(decimals):
"""
Beräknar pi med Machins formel: pi/4 = 4*arctan(1/5) - arctan(1/239)
"""
# Sätt precision
getcontext().prec = decimals + 20
atan1_5 = arctan(5, decimals)
atan1_239 = arctan(239, decimals)
# pi = 4 * (4*arctan(1/5) - arctan(1/239))
pi = Decimal(4) * atan1_5
pi = pi - atan1_239
pi = pi * Decimal(4)
return pi
def main():
# Hämta antal decimaler från argument
if len(sys.argv) > 1:
try:
decimals = int(sys.argv[1])
if decimals < 1:
decimals = 100
except ValueError:
decimals = 100
else:
decimals = 100
# Beräkna pi
pi = calculate_pi(decimals)
# Skriv ut resultatet med rätt antal decimaler
pi_str = str(pi)
# Hitta decimalpunkten
if '.' in pi_str:
int_part, dec_part = pi_str.split('.')
# Ta exakt decimals decimaler
if len(dec_part) > decimals:
dec_part = dec_part[:decimals]
else:
dec_part = dec_part.ljust(decimals, '0')
print(f"{int_part}.{dec_part}")
else:
print(pi_str)
if __name__ == "__main__":
main()
+66
View File
@@ -0,0 +1,66 @@
#!/usr/bin/env python3
"""
Unit tests for Python pi calculation using pytest
"""
import subprocess
import pytest
SCRIPT_PATH = "/Users/einand/Code/test/python/print_hej.py"
def run_script(decimals=None):
"""Run the Python script with optional decimals argument"""
cmd = ["python3", SCRIPT_PATH]
if decimals is not None:
cmd.append(str(decimals))
result = subprocess.run(cmd, capture_output=True, text=True)
return result.stdout.strip()
class TestPiCalculation:
"""Test cases for pi calculation"""
def test_10_decimals(self):
"""Test with 10 decimal places"""
result = run_script(10)
expected = "3.1415926535"
assert result == expected, f"Expected {expected}, got {result}"
def test_5_decimals(self):
"""Test with 5 decimal places"""
result = run_script(5)
expected = "3.14159"
assert result == expected, f"Expected {expected}, got {result}"
def test_1_decimal(self):
"""Test with 1 decimal place"""
result = run_script(1)
expected = "3.1"
assert result == expected, f"Expected {expected}, got {result}"
def test_100_decimals(self):
"""Test with 100 decimal places"""
result = run_script(100)
expected = "3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679"
assert result == expected, f"Expected {expected}, got {result}"
def test_default_100_decimals(self):
"""Test default (100 decimal places)"""
result = run_script()
expected = "3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679"
assert result == expected, f"Expected {expected}, got {result}"
def test_invalid_input(self):
"""Test with invalid input (should use default 100)"""
result = run_script("invalid")
expected = "3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679"
assert result == expected, f"Expected {expected}, got {result}"
def test_10000_decimals(self):
"""Test with 10000 decimal places"""
result = run_script(10000)
# Check length: "3." + 10000 digits = 10002 characters
assert len(result) == 10002, f"Expected 10002 characters, got {len(result)}"
assert result.startswith("3.14159"), f"Result should start with 3.14159, got {result[:10]}"
if __name__ == "__main__":
pytest.main([__file__, "-v"])