Files
print_hej/crystal/src/print_hej.cr
T
Ein Anderssono 54d2fecee0 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
2026-04-23 00:26:18 +02:00

63 lines
1.0 KiB
Crystal

require "big"
def pow10(exp : Int32) : BigInt
result = BigInt.new(1)
exp.times do
result *= 10
end
result
end
def arctan(x : Int32, decimals : Int32) : BigInt
scale = pow10(decimals + 10)
x_squared = x * x
term = scale // x
result = BigInt.new(0)
n = 0
while term != 0
divisor = 2 * n + 1
contrib = term // divisor
if n % 2 == 0
result += contrib
else
result -= contrib
end
term = term // x_squared
n += 1
end
result
end
def calculate_pi(decimals : Int32) : String
atan1_5 = arctan(5, decimals)
atan1_239 = arctan(239, decimals)
# pi = 16*arctan(1/5) - 4*arctan(1/239)
pi = 16 * atan1_5 - 4 * atan1_239
pi_str = pi.to_s
# Format with decimal point
result = "3."
start = 1
decimals.times do |i|
if start + i < pi_str.size
result += pi_str[start + i].to_s
else
result += "0"
end
end
result
end
# Main
decimals = ARGV.size > 0 ? ARGV[0].to_i? || 100 : 100
puts calculate_pi(decimals)