The Computer Language
Benchmarks Game

pidigits C# .NET Core #2 program

source code

/* The Computer Language Benchmarks Game
   http://benchmarksgame.alioth.debian.org/
 *
 * Contributed by Alp Toker <alp@atoker.com>
 * Modified by Antti Lankila <alankila@bel.fi>
 */

using System;
using UBigInteger = Mono.Math.BigInteger;

public class pidigits
{
    const int L = 10;

    public static void Main (string[] args)
    {
	if (args.Length != 1)
	    return;

	int n = Int32.Parse(args[0]);
	int j = 0;

	PiDigitSpigot pds = new PiDigitSpigot();
	while (j < n) {
	    string output = "";
	    for (int i = 0; i != L && j != n; i++, j++)
		output += pds.Next();
            Console.WriteLine("{0,-" + L + "}\t:{1}", output, j);
	}
    }
}

class PiDigitSpigot
{
    private BigInteger z0 = 1, z1 = 0, z2 = 1;
    private int k = 0;

    private int ExtractDigit(int digit)
    {
	return ((z0 * digit + z1) / z2).IntValue();
    }

    private void Compose_k(int k)
    {
	int k2 = 2 * k + 1;
	z1 = k2 * z1 + 2 * k2 * z0;
	z0 *= k;
	z2 *= k2;
    }

    private void Compose_d(int d)
    {
	z1 = 10 * z1 + -10 * d * z2;
	z0 *= 10;
    }

    public int Next()
    {
	int d;
	while (z0 == z2 || (d = ExtractDigit(3)) != ExtractDigit(4))
	    Compose_k(++k);
	Compose_d(d);
	return d;
    }
}

class BigInteger
{
    private UBigInteger bi;
    private int sign = 1;
    private static readonly BigInteger zero = 0;

    private BigInteger () {}

    public static implicit operator BigInteger(int i)
    {
	return new BigInteger(i);
    }

    public BigInteger(int value)
    {
	if (value == 0)
	    sign = 0;
	else if (value < 0) {
	    sign = -1;
	    value = -value;
	}
	bi = new UBigInteger(value);
    }

    public int IntValue()
    {
	return sign * bi.GetBytes()[0];
    }

    public override string ToString ()
    {
	return (sign == -1 ? "-" : "") + bi.ToString ();
    }

    public static BigInteger operator * (BigInteger bi1, BigInteger bi2)
    {
	BigInteger ret = new BigInteger();
	ret.bi = bi1.bi * bi2.bi;
	ret.sign = bi1.sign * bi2.sign;
	return ret;
    }

    public static BigInteger operator / (BigInteger bi1, BigInteger bi2)
    {
	BigInteger ret = new BigInteger();
	ret.bi = bi1.bi / bi2.bi;
	ret.sign = bi1.sign * bi2.sign;
	return ret;
    }

    public static BigInteger operator + (BigInteger bi1, BigInteger bi2)
    {
	if (bi1.sign == 0)
	    return bi2;

	if (bi2.sign == 0)
	    return bi1;
	
	if (bi1.sign == bi2.sign) {
	    BigInteger ret = new BigInteger();
	    ret.bi = bi1.bi + bi2.bi;
	    ret.sign = bi1.sign;
	    return ret;
	}
	
	
	if (bi1.bi == bi2.bi)
	    return zero;

	if (bi1.bi < bi2.bi) {
	    BigInteger ret = new BigInteger();
	    ret.bi = bi2.bi - bi1.bi;
	    ret.sign = bi2.sign;
	    return ret;
	} else {
	    BigInteger ret = new BigInteger();
	    ret.bi = bi1.bi - bi2.bi;
	    ret.sign = -1 * bi2.sign;
	    return ret;
	}
    }
}
    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
2.0.2 a04b4bf512
"System.GC.Server": true


Fri, 27 Oct 2017 01:00:42 GMT

MAKE:
cp pidigits.csharpcore-2.csharpcore Program.cs
cp Include/csharpcore/tmp.csproj .
cp Include/csharpcore/runtimeconfig.template.json .
mkdir obj
cp Include/csharpcore/tmp.csproj.nuget.g.props ./obj
cp Include/csharpcore/tmp.csproj.nuget.g.targets ./obj
/usr/bin/dotnet build -c Release
Microsoft (R) Build Engine version 15.4.8.50001 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

Program.cs(9,21): error CS0246: The type or namespace name 'Mono' could not be found (are you missing a using directive or an assembly reference?) [/home/dunham/benchmarksgame_quadcore/pidigits/tmp/tmp.csproj]

Build FAILED.

Program.cs(9,21): error CS0246: The type or namespace name 'Mono' could not be found (are you missing a using directive or an assembly reference?) [/home/dunham/benchmarksgame_quadcore/pidigits/tmp/tmp.csproj]
    0 Warning(s)
    1 Error(s)

Time Elapsed 00:00:02.78
/home/dunham/benchmarksgame/nanobench/makefiles/u64q.programs.Makefile:224: recipe for target 'pidigits.csharpcore-2.csharpcore_run' failed
make: [pidigits.csharpcore-2.csharpcore_run] Error 1 (ignored)

5.40s to complete and log all make actions

COMMAND LINE:
/usr/bin/dotnet ./bin/Release/netcoreapp2.0/tmp.dll 2000

PROGRAM FAILED 


PROGRAM OUTPUT:

No executable found matching command "dotnet-./bin/Release/netcoreapp2.0/tmp.dll"