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
+35
@@ -0,0 +1,35 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Java Build Script
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "$0")/.." && pwd)"
|
||||
cd "$SCRIPT_DIR"
|
||||
|
||||
echo "=== Java Build ==="
|
||||
echo ""
|
||||
|
||||
# Kompilera Java-programmet
|
||||
cd src
|
||||
javac print_hej.java
|
||||
|
||||
if [ $? -eq 0 ]; then
|
||||
echo "✓ Kompilering lyckades!"
|
||||
echo "Class: src/print_hej.class"
|
||||
echo ""
|
||||
echo "För att köra:"
|
||||
echo " java -cp src print_hej [decimaler]"
|
||||
|
||||
# Skapa wrapper script
|
||||
mkdir -p ../bin
|
||||
cat > ../bin/print_hej << 'EOF'
|
||||
#!/bin/bash
|
||||
SCRIPT_DIR="$(cd "$(dirname "$0")/.." && pwd)"
|
||||
cd "$SCRIPT_DIR"
|
||||
java -cp src print_hej "$@"
|
||||
EOF
|
||||
chmod +x ../bin/print_hej
|
||||
echo "Wrapper script skapad: bin/print_hej"
|
||||
else
|
||||
echo "✗ Kompilering misslyckades!"
|
||||
exit 1
|
||||
fi
|
||||
@@ -0,0 +1,65 @@
|
||||
import java.math.BigInteger;
|
||||
|
||||
public class print_hej {
|
||||
public static void main(String[] args) {
|
||||
int decimals = 100;
|
||||
if (args.length > 0) {
|
||||
try {
|
||||
decimals = Integer.parseInt(args[0]);
|
||||
if (decimals <= 0) decimals = 100;
|
||||
} catch (NumberFormatException e) {
|
||||
decimals = 100;
|
||||
}
|
||||
}
|
||||
|
||||
BigInteger pi = calculatePi(decimals);
|
||||
System.out.println(formatPi(pi, decimals));
|
||||
}
|
||||
|
||||
// Calculate arctan(1/x) using Taylor series
|
||||
static BigInteger arctan(int x, int decimals) {
|
||||
BigInteger scale = BigInteger.TEN.pow(decimals + 10);
|
||||
BigInteger xBig = BigInteger.valueOf(x);
|
||||
BigInteger xSquared = xBig.multiply(xBig);
|
||||
|
||||
BigInteger result = BigInteger.ZERO;
|
||||
BigInteger term = scale.divide(xBig);
|
||||
|
||||
int n = 0;
|
||||
while (term.compareTo(BigInteger.ZERO) != 0 && n < decimals * 3) {
|
||||
BigInteger divisor = BigInteger.valueOf(2 * n + 1);
|
||||
BigInteger contrib = term.divide(divisor);
|
||||
|
||||
if (n % 2 == 0) {
|
||||
result = result.add(contrib);
|
||||
} else {
|
||||
result = result.subtract(contrib);
|
||||
}
|
||||
|
||||
term = term.divide(xSquared);
|
||||
n++;
|
||||
|
||||
if (n > decimals * 2) break;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// Calculate pi using Machin's formula
|
||||
static BigInteger calculatePi(int decimals) {
|
||||
BigInteger atan1_5 = arctan(5, decimals);
|
||||
BigInteger atan1_239 = arctan(239, decimals);
|
||||
|
||||
return atan1_5.multiply(BigInteger.valueOf(16))
|
||||
.subtract(atan1_239.multiply(BigInteger.valueOf(4)));
|
||||
}
|
||||
|
||||
// Format pi with decimal point
|
||||
static String formatPi(BigInteger pi, int decimals) {
|
||||
String piStr = pi.toString();
|
||||
while (piStr.length() < decimals + 10) {
|
||||
piStr = "0" + piStr;
|
||||
}
|
||||
return "3." + piStr.substring(1, decimals + 1);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user