WeixiangYan/CodeTransOcean huggingface dataset:
Task: Probabilistic choice




Go: package main

import (
    "fmt"
    "math/rand"
    "time"
)

type mapping struct {
    item string
    pr   float64
}

func main() {
    
    m := []mapping{
        {"aleph", 1 / 5.},
        {"beth", 1 / 6.},
        {"gimel", 1 / 7.},
        {"daleth", 1 / 8.},
        {"he", 1 / 9.},
        {"waw", 1 / 10.},
        {"zayin", 1 / 11.},
        {"heth", 1759 / 27720.}} 

    
    cpr := make([]float64, len(m)-1)
    var c float64
    for i := 0; i < len(m)-1; i++ {
        c += m[i].pr
        cpr[i] = c
    }

    
    const samples = 1e6
    occ := make([]int, len(m))
    rand.Seed(time.Now().UnixNano())
    for i := 0; i < samples; i++ {
        r := rand.Float64()
        for j := 0; ; j++ {
            if r < cpr[j] {
                occ[j]++
                break
            }
            if j == len(cpr)-1 {
                occ[len(cpr)]++
                break
            }
        }
    }

    
    fmt.Println("  Item  Target   Generated")
    var totalTarget, totalGenerated float64
    for i := 0; i < len(m); i++ {
        target := m[i].pr
        generated := float64(occ[i]) / samples
        fmt.Printf("%6s  %8.6f  %8.6f\n", m[i].item, target, generated)
        totalTarget += target
        totalGenerated += generated
    }
    fmt.Printf("Totals  %8.6f  %8.6f\n", totalTarget, totalGenerated)
}




Python: import random, bisect

def probchoice(items, probs):
  
  
  prob_accumulator = 0
  accumulator = []
  for p in probs:
    prob_accumulator += p
    accumulator.append(prob_accumulator)
    
  while True:
    r = random.random()
    yield items[bisect.bisect(accumulator, r)]

def probchoice2(items, probs, bincount=10000):
  
  
  bins = []
  for item,prob in zip(items, probs):
    bins += [item]*int(bincount*prob)
  while True:
    yield random.choice(bins)
      
      
def tester(func=probchoice, items='good bad ugly'.split(),
                    probs=[0.5, 0.3, 0.2],
                    trials = 100000
                    ):
  def problist2string(probs):
    
    return ",".join('%8.6f' % (p,) for p in probs)
  
  from collections import defaultdict
   
  counter = defaultdict(int)
  it = func(items, probs)
  for dummy in xrange(trials):
    counter[it.next()] += 1
  print "\n
  print "Trials:              ", trials
  print "Items:               ", ' '.join(items)
  print "Target probability:  ", problist2string(probs)
  print "Attained probability:", problist2string(
    counter[x]/float(trials) for x in items)

if __name__ == '__main__':
  items = 'aleph beth gimel daleth he waw zayin heth'.split()
  probs = [1/(float(n)+5) for n in range(len(items))]
  probs[-1] = 1-sum(probs[:-1])
  tester(probchoice, items, probs, 1000000)
  tester(probchoice2, items, probs, 1000000)
Task: Probabilistic choice







Python: import random, bisect

def probchoice(items, probs):
  
  
  prob_accumulator = 0
  accumulator = []
  for p in probs:
    prob_accumulator += p
    accumulator.append(prob_accumulator)
    
  while True:
    r = random.random()
    yield items[bisect.bisect(accumulator, r)]

def probchoice2(items, probs, bincount=10000):
  
  
  bins = []
  for item,prob in zip(items, probs):
    bins += [item]*int(bincount*prob)
  while True:
    yield random.choice(bins)
      
      
def tester(func=probchoice, items='good bad ugly'.split(),
                    probs=[0.5, 0.3, 0.2],
                    trials = 100000
                    ):
  def problist2string(probs):
    
    return ",".join('%8.6f' % (p,) for p in probs)
  
  from collections import defaultdict
   
  counter = defaultdict(int)
  it = func(items, probs)
  for dummy in xrange(trials):
    counter[it.next()] += 1
  print "\n
  print "Trials:              ", trials
  print "Items:               ", ' '.join(items)
  print "Target probability:  ", problist2string(probs)
  print "Attained probability:", problist2string(
    counter[x]/float(trials) for x in items)

if __name__ == '__main__':
  items = 'aleph beth gimel daleth he waw zayin heth'.split()
  probs = [1/(float(n)+5) for n in range(len(items))]
  probs[-1] = 1-sum(probs[:-1])
  tester(probchoice, items, probs, 1000000)
  tester(probchoice2, items, probs, 1000000)


VB: item = Array("aleph","beth","gimel","daleth","he","waw","zayin","heth")
prob = Array(1/5.0, 1/6.0, 1/7.0, 1/8.0, 1/9.0, 1/10.0, 1/11.0, 1759/27720)
Dim cnt(7)


sum = 0
For i = 0 To UBound(prob)
	sum = sum + prob(i)
Next

If sum <> 1 Then
	WScript.Quit
End If

For trial = 1 To 1000000
	r = Rnd(1)
	p = 0
	For i = 0 To UBound(prob)
		p = p + prob(i)
		If r < p Then
			cnt(i) = cnt(i) + 1
			Exit For
		End If
	Next
Next

WScript.StdOut.Write "item" & vbTab & "actual" & vbTab & vbTab & "theoretical"
WScript.StdOut.WriteLine
For i = 0 To UBound(item)
	WScript.StdOut.Write item(i) & vbTab & FormatNumber(cnt(i)/1000000,6) & vbTab & FormatNumber(prob(i),6)
	WScript.StdOut.WriteLine
Next
Task: Coprimes

C: #include <stdio.h>

int gcd(int a, int b) {
    int c;
    while (b) {
        c = a;
        a = b;
        b = c % b;
    }
    return a;
}

struct pair {
    int x, y;
};

void printPair(struct pair const *p) {
    printf("{%d, %d}\n", p->x, p->y);
}

int main() {
    struct pair pairs[] = {
        {21,15}, {17,23}, {36,12}, {18,29}, {60,15}
    };
    
    int i;
    for (i=0; i<5; i++) {
        if (gcd(pairs[i].x, pairs[i].y) == 1)
            printPair(&pairs[i]);
    }
    return 0;
}



C#: #include <iostream>
#include <algorithm>
#include <vector>
#include <utility>

int gcd(int a, int b) {
    int c;
    while (b) {
        c = a;
        a = b;
        b = c % b;
    }
    return a;
}

int main() {
    using intpair = std::pair<int,int>;
    std::vector<intpair> pairs = {
        {21,15}, {17,23}, {36,12}, {18,29}, {60,15}
    };
    
    pairs.erase(
        std::remove_if(
            pairs.begin(),
            pairs.end(),
            [](const intpair& x) {
                return gcd(x.first, x.second) != 1;
            }
        ),
        pairs.end()
    );
    
    for (auto& x : pairs) {
        std::cout << "{" << x.first 
                  << ", " << x.second 
                  << "}" << std::endl;
    }
    
    return 0;
}
Task: Coprimes

C: #include <stdio.h>

int gcd(int a, int b) {
    int c;
    while (b) {
        c = a;
        a = b;
        b = c % b;
    }
    return a;
}

struct pair {
    int x, y;
};

void printPair(struct pair const *p) {
    printf("{%d, %d}\n", p->x, p->y);
}

int main() {
    struct pair pairs[] = {
        {21,15}, {17,23}, {36,12}, {18,29}, {60,15}
    };
    
    int i;
    for (i=0; i<5; i++) {
        if (gcd(pairs[i].x, pairs[i].y) == 1)
            printPair(&pairs[i]);
    }
    return 0;
}


C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <utility>

int gcd(int a, int b) {
    int c;
    while (b) {
        c = a;
        a = b;
        b = c % b;
    }
    return a;
}

int main() {
    using intpair = std::pair<int,int>;
    std::vector<intpair> pairs = {
        {21,15}, {17,23}, {36,12}, {18,29}, {60,15}
    };
    
    pairs.erase(
        std::remove_if(
            pairs.begin(),
            pairs.end(),
            [](const intpair& x) {
                return gcd(x.first, x.second) != 1;
            }
        ),
        pairs.end()
    );
    
    for (auto& x : pairs) {
        std::cout << "{" << x.first 
                  << ", " << x.second 
                  << "}" << std::endl;
    }
    
    return 0;
}
Task: Coprimes

C: #include <stdio.h>

int gcd(int a, int b) {
    int c;
    while (b) {
        c = a;
        a = b;
        b = c % b;
    }
    return a;
}

struct pair {
    int x, y;
};

void printPair(struct pair const *p) {
    printf("{%d, %d}\n", p->x, p->y);
}

int main() {
    struct pair pairs[] = {
        {21,15}, {17,23}, {36,12}, {18,29}, {60,15}
    };
    
    int i;
    for (i=0; i<5; i++) {
        if (gcd(pairs[i].x, pairs[i].y) == 1)
            printPair(&pairs[i]);
    }
    return 0;
}




Go: package main

import (
    "fmt"
    "rcu"
)

func main() {
    pairs := [][2]int{{21, 15}, {17, 23}, {36, 12}, {18, 29}, {60, 15}}
    fmt.Println("The following pairs of numbers are coprime:")
    for _, pair := range pairs {
        if rcu.Gcd(pair[0], pair[1]) == 1 {
            fmt.Println(pair)
        }
    }
}
Task: Coprimes


C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <utility>

int gcd(int a, int b) {
    int c;
    while (b) {
        c = a;
        a = b;
        b = c % b;
    }
    return a;
}

int main() {
    using intpair = std::pair<int,int>;
    std::vector<intpair> pairs = {
        {21,15}, {17,23}, {36,12}, {18,29}, {60,15}
    };
    
    pairs.erase(
        std::remove_if(
            pairs.begin(),
            pairs.end(),
            [](const intpair& x) {
                return gcd(x.first, x.second) != 1;
            }
        ),
        pairs.end()
    );
    
    for (auto& x : pairs) {
        std::cout << "{" << x.first 
                  << ", " << x.second 
                  << "}" << std::endl;
    }
    
    return 0;
}



Go: package main

import (
    "fmt"
    "rcu"
)

func main() {
    pairs := [][2]int{{21, 15}, {17, 23}, {36, 12}, {18, 29}, {60, 15}}
    fmt.Println("The following pairs of numbers are coprime:")
    for _, pair := range pairs {
        if rcu.Gcd(pair[0], pair[1]) == 1 {
            fmt.Println(pair)
        }
    }
}
Task: Coprimes


C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <utility>

int gcd(int a, int b) {
    int c;
    while (b) {
        c = a;
        a = b;
        b = c % b;
    }
    return a;
}

int main() {
    using intpair = std::pair<int,int>;
    std::vector<intpair> pairs = {
        {21,15}, {17,23}, {36,12}, {18,29}, {60,15}
    };
    
    pairs.erase(
        std::remove_if(
            pairs.begin(),
            pairs.end(),
            [](const intpair& x) {
                return gcd(x.first, x.second) != 1;
            }
        ),
        pairs.end()
    );
    
    for (auto& x : pairs) {
        std::cout << "{" << x.first 
                  << ", " << x.second 
                  << "}" << std::endl;
    }
    
    return 0;
}






Python: 

from math import gcd



def coprime(a, b):
    
    return 1 == gcd(a, b)




def main():
    

    print([
        xy for xy in [
            (21, 15), (17, 23), (36, 12),
            (18, 29), (60, 15)
        ]
        if coprime(*xy)
    ])



if __name__ == '__main__':
    main()
Task: Coprimes




Go: package main

import (
    "fmt"
    "rcu"
)

func main() {
    pairs := [][2]int{{21, 15}, {17, 23}, {36, 12}, {18, 29}, {60, 15}}
    fmt.Println("The following pairs of numbers are coprime:")
    for _, pair := range pairs {
        if rcu.Gcd(pair[0], pair[1]) == 1 {
            fmt.Println(pair)
        }
    }
}




Python: 

from math import gcd



def coprime(a, b):
    
    return 1 == gcd(a, b)




def main():
    

    print([
        xy for xy in [
            (21, 15), (17, 23), (36, 12),
            (18, 29), (60, 15)
        ]
        if coprime(*xy)
    ])



if __name__ == '__main__':
    main()
Task: Coprimes

C: #include <stdio.h>

int gcd(int a, int b) {
    int c;
    while (b) {
        c = a;
        a = b;
        b = c % b;
    }
    return a;
}

struct pair {
    int x, y;
};

void printPair(struct pair const *p) {
    printf("{%d, %d}\n", p->x, p->y);
}

int main() {
    struct pair pairs[] = {
        {21,15}, {17,23}, {36,12}, {18,29}, {60,15}
    };
    
    int i;
    for (i=0; i<5; i++) {
        if (gcd(pairs[i].x, pairs[i].y) == 1)
            printPair(&pairs[i]);
    }
    return 0;
}







Python: 

from math import gcd



def coprime(a, b):
    
    return 1 == gcd(a, b)




def main():
    

    print([
        xy for xy in [
            (21, 15), (17, 23), (36, 12),
            (18, 29), (60, 15)
        ]
        if coprime(*xy)
    ])



if __name__ == '__main__':
    main()
Task: Arithmetic-geometric mean_Calculate Pi

C: #include "gmp.h"

void agm (const mpf_t in1, const mpf_t in2, mpf_t out1, mpf_t out2) {
	mpf_add (out1, in1, in2);
	mpf_div_ui (out1, out1, 2);
	mpf_mul (out2, in1, in2);
	mpf_sqrt (out2, out2);
}

int main (void) {
	mpf_set_default_prec (300000);
	mpf_t x0, y0, resA, resB, Z, var;

	mpf_init_set_ui (x0, 1);
	mpf_init_set_d (y0, 0.5);
	mpf_sqrt (y0, y0);
	mpf_init (resA);
	mpf_init (resB);
	mpf_init_set_d (Z, 0.25);
	mpf_init (var);

	int n = 1;
        int i;
	for(i=0; i<8; i++){
		agm(x0, y0, resA, resB);
		mpf_sub(var, resA, x0);
		mpf_mul(var, var, var);
		mpf_mul_ui(var, var, n);
		mpf_sub(Z, Z, var);
		n += n;
		agm(resA, resB, x0, y0);
		mpf_sub(var, x0, resA);
		mpf_mul(var, var, var);
		mpf_mul_ui(var, var, n);
		mpf_sub(Z, Z, var);
		n += n;
	}
	mpf_mul(x0, x0, x0);
	mpf_div(x0, x0, Z);
	gmp_printf ("%.100000Ff\n", x0);
	return 0;
}



C#: using System;
using System.Numerics;

class AgmPie
{
    static BigInteger IntSqRoot(BigInteger valu, BigInteger guess)
    {
        BigInteger term; do {
            term = valu / guess; if (BigInteger.Abs(term - guess) <= 1) break;
            guess += term; guess >>= 1;
        } while (true); return guess;
    }

    static BigInteger ISR(BigInteger term, BigInteger guess)
    {
        BigInteger valu = term * guess; do {
            if (BigInteger.Abs(term - guess) <= 1) break;
            guess += term; guess >>= 1; term = valu / guess;
        } while (true); return guess;
    }

    static BigInteger CalcAGM(BigInteger lam, BigInteger gm, ref BigInteger z,
                              BigInteger ep)
    {
        BigInteger am, zi; ulong n = 1; do {
            am = (lam + gm) >> 1; gm = ISR(lam, gm);
            BigInteger v = am - lam; if ((zi = v * v * n) < ep) break;
            z -= zi; n <<= 1; lam = am;
        } while (true); return am;
    }

    static BigInteger BIP(int exp, ulong man = 1) 
    {
        BigInteger rv = BigInteger.Pow(10, exp); return man == 1 ? rv : man * rv;
    }

    static void Main(string[] args)
    {
        int d = 25000;
        if (args.Length > 0)
        {
            int.TryParse(args[0], out d);
            if (d < 1 || d > 999999) d = 25000;
        }
        DateTime st = DateTime.Now;
        BigInteger am = BIP(d),
          gm = IntSqRoot(BIP(d + d - 1, 5),
                             BIP(d - 15, (ulong)(Math.Sqrt(0.5) * 1e+15))),
          z = BIP(d + d - 2, 25),
          agm = CalcAGM(am, gm, ref z, BIP(d + 1)),
          pi = agm * agm * BIP(d - 2) / z;
        Console.WriteLine("Computation time: {0:0.0000} seconds ",
                             (DateTime.Now - st).TotalMilliseconds / 1000);
        string s = pi.ToString();
        Console.WriteLine("{0}.{1}", s[0], s.Substring(1));
        if (System.Diagnostics.Debugger.IsAttached) Console.ReadKey();
    }
}
Task: Arithmetic-geometric mean_Calculate Pi

C: #include "gmp.h"

void agm (const mpf_t in1, const mpf_t in2, mpf_t out1, mpf_t out2) {
	mpf_add (out1, in1, in2);
	mpf_div_ui (out1, out1, 2);
	mpf_mul (out2, in1, in2);
	mpf_sqrt (out2, out2);
}

int main (void) {
	mpf_set_default_prec (300000);
	mpf_t x0, y0, resA, resB, Z, var;

	mpf_init_set_ui (x0, 1);
	mpf_init_set_d (y0, 0.5);
	mpf_sqrt (y0, y0);
	mpf_init (resA);
	mpf_init (resB);
	mpf_init_set_d (Z, 0.25);
	mpf_init (var);

	int n = 1;
        int i;
	for(i=0; i<8; i++){
		agm(x0, y0, resA, resB);
		mpf_sub(var, resA, x0);
		mpf_mul(var, var, var);
		mpf_mul_ui(var, var, n);
		mpf_sub(Z, Z, var);
		n += n;
		agm(resA, resB, x0, y0);
		mpf_sub(var, x0, resA);
		mpf_mul(var, var, var);
		mpf_mul_ui(var, var, n);
		mpf_sub(Z, Z, var);
		n += n;
	}
	mpf_mul(x0, x0, x0);
	mpf_div(x0, x0, Z);
	gmp_printf ("%.100000Ff\n", x0);
	return 0;
}


C++: #include <gmpxx.h>
#include <chrono>

using namespace std;
using namespace chrono;
 
void agm(mpf_class& rop1, mpf_class& rop2, const mpf_class& op1,
        const mpf_class& op2)
{
    rop1 = (op1 + op2) / 2;
    rop2 = op1 * op2;
    mpf_sqrt(rop2.get_mpf_t(), rop2.get_mpf_t());
}
 
int main(void)
{
    auto st = steady_clock::now();
    mpf_set_default_prec(300000);
    mpf_class x0, y0, resA, resB, Z;
 
    x0 = 1;
    y0 = 0.5;
    Z  = 0.25;
    mpf_sqrt(y0.get_mpf_t(), y0.get_mpf_t());
 
    int n = 1;
    for (int i = 0; i < 8; i++) {
        agm(resA, resB, x0, y0);
        Z -= n * (resA - x0) * (resA - x0);
        n *= 2;
 
        agm(x0, y0, resA, resB);
        Z -= n * (x0 - resA) * (x0 - resA);
        n *= 2;
    }
 
    x0 = x0 * x0 / Z;
    printf("Took %f ms for computation.\n", duration<double>(steady_clock::now() - st).count() * 1000.0);
    st = steady_clock::now();
    gmp_printf ("%.89412Ff\n", x0.get_mpf_t());
    printf("Took %f ms for output.\n", duration<double>(steady_clock::now() - st).count() * 1000.0);
    return 0;
}
Task: Arithmetic-geometric mean_Calculate Pi

C: #include "gmp.h"

void agm (const mpf_t in1, const mpf_t in2, mpf_t out1, mpf_t out2) {
	mpf_add (out1, in1, in2);
	mpf_div_ui (out1, out1, 2);
	mpf_mul (out2, in1, in2);
	mpf_sqrt (out2, out2);
}

int main (void) {
	mpf_set_default_prec (300000);
	mpf_t x0, y0, resA, resB, Z, var;

	mpf_init_set_ui (x0, 1);
	mpf_init_set_d (y0, 0.5);
	mpf_sqrt (y0, y0);
	mpf_init (resA);
	mpf_init (resB);
	mpf_init_set_d (Z, 0.25);
	mpf_init (var);

	int n = 1;
        int i;
	for(i=0; i<8; i++){
		agm(x0, y0, resA, resB);
		mpf_sub(var, resA, x0);
		mpf_mul(var, var, var);
		mpf_mul_ui(var, var, n);
		mpf_sub(Z, Z, var);
		n += n;
		agm(resA, resB, x0, y0);
		mpf_sub(var, x0, resA);
		mpf_mul(var, var, var);
		mpf_mul_ui(var, var, n);
		mpf_sub(Z, Z, var);
		n += n;
	}
	mpf_mul(x0, x0, x0);
	mpf_div(x0, x0, Z);
	gmp_printf ("%.100000Ff\n", x0);
	return 0;
}





Java: import java.math.BigDecimal;
import java.math.MathContext;
import java.util.Objects;

public class Calculate_Pi {
    private static final MathContext con1024 = new MathContext(1024);
    private static final BigDecimal bigTwo = new BigDecimal(2);
    private static final BigDecimal bigFour = new BigDecimal(4);

    private static BigDecimal bigSqrt(BigDecimal bd, MathContext con) {
        BigDecimal x0 = BigDecimal.ZERO;
        BigDecimal x1 = BigDecimal.valueOf(Math.sqrt(bd.doubleValue()));
        while (!Objects.equals(x0, x1)) {
            x0 = x1;
            x1 = bd.divide(x0, con).add(x0).divide(bigTwo, con);
        }
        return x1;
    }

    public static void main(String[] args) {
        BigDecimal a = BigDecimal.ONE;
        BigDecimal g = a.divide(bigSqrt(bigTwo, con1024), con1024);
        BigDecimal t;
        BigDecimal sum = BigDecimal.ZERO;
        BigDecimal pow = bigTwo;
        while (!Objects.equals(a, g)) {
            t = a.add(g).divide(bigTwo, con1024);
            g = bigSqrt(a.multiply(g), con1024);
            a = t;
            pow = pow.multiply(bigTwo);
            sum = sum.add(a.multiply(a).subtract(g.multiply(g)).multiply(pow));
        }
        BigDecimal pi = bigFour.multiply(a.multiply(a)).divide(BigDecimal.ONE.subtract(sum), con1024);
        System.out.println(pi);
    }
}
Task: Arithmetic-geometric mean_Calculate Pi


C++: #include <gmpxx.h>
#include <chrono>

using namespace std;
using namespace chrono;
 
void agm(mpf_class& rop1, mpf_class& rop2, const mpf_class& op1,
        const mpf_class& op2)
{
    rop1 = (op1 + op2) / 2;
    rop2 = op1 * op2;
    mpf_sqrt(rop2.get_mpf_t(), rop2.get_mpf_t());
}
 
int main(void)
{
    auto st = steady_clock::now();
    mpf_set_default_prec(300000);
    mpf_class x0, y0, resA, resB, Z;
 
    x0 = 1;
    y0 = 0.5;
    Z  = 0.25;
    mpf_sqrt(y0.get_mpf_t(), y0.get_mpf_t());
 
    int n = 1;
    for (int i = 0; i < 8; i++) {
        agm(resA, resB, x0, y0);
        Z -= n * (resA - x0) * (resA - x0);
        n *= 2;
 
        agm(x0, y0, resA, resB);
        Z -= n * (x0 - resA) * (x0 - resA);
        n *= 2;
    }
 
    x0 = x0 * x0 / Z;
    printf("Took %f ms for computation.\n", duration<double>(steady_clock::now() - st).count() * 1000.0);
    st = steady_clock::now();
    gmp_printf ("%.89412Ff\n", x0.get_mpf_t());
    printf("Took %f ms for output.\n", duration<double>(steady_clock::now() - st).count() * 1000.0);
    return 0;
}


C#: using System;
using System.Numerics;

class AgmPie
{
    static BigInteger IntSqRoot(BigInteger valu, BigInteger guess)
    {
        BigInteger term; do {
            term = valu / guess; if (BigInteger.Abs(term - guess) <= 1) break;
            guess += term; guess >>= 1;
        } while (true); return guess;
    }

    static BigInteger ISR(BigInteger term, BigInteger guess)
    {
        BigInteger valu = term * guess; do {
            if (BigInteger.Abs(term - guess) <= 1) break;
            guess += term; guess >>= 1; term = valu / guess;
        } while (true); return guess;
    }

    static BigInteger CalcAGM(BigInteger lam, BigInteger gm, ref BigInteger z,
                              BigInteger ep)
    {
        BigInteger am, zi; ulong n = 1; do {
            am = (lam + gm) >> 1; gm = ISR(lam, gm);
            BigInteger v = am - lam; if ((zi = v * v * n) < ep) break;
            z -= zi; n <<= 1; lam = am;
        } while (true); return am;
    }

    static BigInteger BIP(int exp, ulong man = 1) 
    {
        BigInteger rv = BigInteger.Pow(10, exp); return man == 1 ? rv : man * rv;
    }

    static void Main(string[] args)
    {
        int d = 25000;
        if (args.Length > 0)
        {
            int.TryParse(args[0], out d);
            if (d < 1 || d > 999999) d = 25000;
        }
        DateTime st = DateTime.Now;
        BigInteger am = BIP(d),
          gm = IntSqRoot(BIP(d + d - 1, 5),
                             BIP(d - 15, (ulong)(Math.Sqrt(0.5) * 1e+15))),
          z = BIP(d + d - 2, 25),
          agm = CalcAGM(am, gm, ref z, BIP(d + 1)),
          pi = agm * agm * BIP(d - 2) / z;
        Console.WriteLine("Computation time: {0:0.0000} seconds ",
                             (DateTime.Now - st).TotalMilliseconds / 1000);
        string s = pi.ToString();
        Console.WriteLine("{0}.{1}", s[0], s.Substring(1));
        if (System.Diagnostics.Debugger.IsAttached) Console.ReadKey();
    }
}