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
+88
@@ -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()
|
||||
Reference in New Issue
Block a user