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
+28
View File
@@ -0,0 +1,28 @@
#!/bin/bash
# Perl Build Script - Perl är interpreterat, ingen kompilering behövs
SCRIPT_DIR="$(cd "$(dirname "$0")/.." && pwd)"
cd "$SCRIPT_DIR"
echo "=== Perl Build ==="
echo "Perl är ett interpreterat språk, ingen kompilering behövs."
echo "Script: src/print_hej.pl"
echo ""
# Skapa wrapper script
mkdir -p bin
cat > bin/print_hej << 'EOF'
#!/bin/bash
SCRIPT_DIR="$(cd "$(dirname "$0")/.." && pwd)"
cd "$SCRIPT_DIR"
perl src/print_hej.pl "$@"
EOF
chmod +x bin/print_hej
echo "Wrapper script skapad: bin/print_hej"
echo ""
echo "För att köra:"
echo " ./bin/print_hej [decimaler]"
echo ""
echo "✓ Klart!"
+14
View File
@@ -0,0 +1,14 @@
#!/bin/bash
# Perl Unit Tests
SCRIPT_DIR="$(cd "$(dirname "$0")/.." && pwd)"
cd "$SCRIPT_DIR"
echo "=== Perl Pi-beräkning Unit Tester ==="
echo ""
cd src
perl test_pi.t
exit $?
+67
View File
@@ -0,0 +1,67 @@
#!/usr/bin/perl
use strict;
use warnings;
use Math::BigInt;
# Calculate arctan(1/x) using Taylor series
sub arctan {
my ($x, $decimals) = @_;
my $scale = Math::BigInt->new(10)->bpow($decimals + 10);
my $x_squared = $x * $x;
my $term = $scale->copy()->bdiv($x);
my $result = Math::BigInt->new(0);
my $n = 0;
while ($term > 0) {
my $divisor = 2 * $n + 1;
my $contrib = $term->copy()->bdiv($divisor);
if ($n % 2 == 0) {
$result->badd($contrib);
} else {
$result->bsub($contrib);
}
$term = $term->copy()->bdiv($x_squared);
$n++;
}
return $result;
}
# Calculate pi using Machin's formula
sub calculate_pi {
my ($decimals) = @_;
my $atan1_5 = arctan(5, $decimals);
my $atan1_239 = arctan(239, $decimals);
# pi = 16*arctan(1/5) - 4*arctan(1/239)
my $pi = $atan1_5->copy()->bmul(16)->bsub($atan1_239->copy()->bmul(4));
# Format with decimal point
my $pi_str = $pi->bstr();
my $result = "3.";
my $start = 1;
for (my $i = 0; $i < $decimals; $i++) {
if ($start + $i < length($pi_str)) {
$result .= substr($pi_str, $start + $i, 1);
} else {
$result .= "0";
}
}
return $result;
}
# Main
my $decimals = 100;
if (@ARGV > 0) {
$decimals = $ARGV[0] =~ /^\d+$/ ? $ARGV[0] : 100;
}
print calculate_pi($decimals) . "\n";
+73
View File
@@ -0,0 +1,73 @@
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More tests => 7;
use IPC::Open3;
my $SCRIPT_PATH = '/Users/einand/Code/test/perl/print_hej.pl';
sub run_script {
my ($args) = @_;
my @cmd = ($SCRIPT_PATH);
push @cmd, $args if defined $args;
my $pid = open3(my $in, my $out, my $err, @cmd);
close($in);
my $result = <$out>;
close($out);
close($err);
waitpid($pid, 0);
chomp $result if defined $result;
return $result // '';
}
# Test 10 decimals
{
my $result = run_script('10');
my $expected = '3.1415926535';
is($result, $expected, '10 decimals');
}
# Test 5 decimals
{
my $result = run_script('5');
my $expected = '3.14159';
is($result, $expected, '5 decimals');
}
# Test 1 decimal
{
my $result = run_script('1');
my $expected = '3.1';
is($result, $expected, '1 decimal');
}
# Test 100 decimals
{
my $result = run_script('100');
my $expected = '3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679';
is($result, $expected, '100 decimals');
}
# Test default 100 decimals
{
my $result = run_script();
my $expected = '3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679';
is($result, $expected, 'default 100 decimals');
}
# Test invalid input
{
my $result = run_script('invalid');
my $expected = '3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679';
is($result, $expected, 'invalid input uses default');
}
# Test 10000 decimals
{
my $result = run_script('10000');
# Check length: "3." + 10000 digits = 10002 characters
is(length($result), 10002, '10000 decimals length');
ok(index($result, '3.14159') == 0, '10000 decimals starts with 3.14159');
}