The Computer Language
Benchmarks Game

pidigits Dart program

source code

/* The Computer Language Benchmarks Game
   http://benchmarksgame.alioth.debian.org/

   contributed by S├ębastien Arnaud
*/

import 'package:bignum/bignum.dart';
import 'package:args/args.dart';

String pad(i, last) {
  var res = i.toString(), count;
  count = 10 - res.length;
  while (count > 0) {
    last ? res += ' ' : res = '0' + res;
    count--;
  }
  return res;
}

void calculatePi(N) {
  var i = 0, ns = 0;

  BigInteger bigint_TEN = new BigInteger(10);
  BigInteger k = BigInteger.ZERO;
  BigInteger k1 = BigInteger.ONE;
  BigInteger a = BigInteger.ZERO;
  BigInteger d = BigInteger.ONE;
  BigInteger m = BigInteger.ZERO;
  BigInteger n = BigInteger.ONE;
  BigInteger t = BigInteger.ZERO;
  BigInteger u = BigInteger.ZERO;

  while (true) {
    k = k.add(BigInteger.ONE);
    k1 = k1.add(BigInteger.TWO);
    t = n.shiftLeft(1);
    n = n.multiply(k);
    a = a.add(t).multiply(k1);
    d = d.multiply(k1);

    if (a.compareTo(n) >= 0) {
      m = n.multiply(BigInteger.THREE).add(a);
      t = m.divide(d);
      u = m.mod(d).add(n);

      if (d.compareTo(u) > 0) {
        ns = ns * 10 + t.intValue();
        i += 1;

        var last = i >= N;
        if (i % 10 == 0 || last) {
          print(pad(ns, last) + '\t:$i');
          ns = 0;
        }

        if (last) break;

        a = a.subtract(d.multiply(t)).multiply(bigint_TEN);
        n = n.multiply(bigint_TEN);
      }
    }
  }
}

void main(List<String> arguments) {
  ArgResults argResults;
  ArgParser parser = new ArgParser();
  argResults = parser.parse(arguments);

  int N = int.parse(argResults.rest[0]);
  calculatePi(N);
}
    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
Dart VM version: 1.22.0 (Mon Feb 13 17:43:03 2017) on "linux_x64"


Wed, 15 Feb 2017 21:51:18 GMT

MAKE:
mv pidigits.dart pidigits.dart
mv: 'pidigits.dart' and 'pidigits.dart' are the same file
/home/dunham/benchmarksgame/nanobench/makefiles/u64q.programs.Makefile:687: recipe for target 'pidigits.dart_run' failed
make: [pidigits.dart_run] Error 1 (ignored)
/usr/local/src/dart-sdk/bin/pub get
Resolving dependencies...
Could not resolve URL "https://pub.dartlang.org".
/home/dunham/benchmarksgame/nanobench/makefiles/u64q.programs.Makefile:687: recipe for target 'pidigits.dart_run' failed
make: [pidigits.dart_run] Error 69 (ignored)
0.81s to complete and log all make actions

COMMAND LINE:
/usr/local/src/dart-sdk/bin/dart  pidigits.dart 2000

PROGRAM FAILED 


PROGRAM OUTPUT:

Unhandled exception:
Could not import "package:args/args.dart" from "file:///home/dunham/benchmarksgame_quadcore/pidigits/tmp/pidigits.dart": Could not resolve a package location for base at file:///home/dunham/benchmarksgame_quadcore/pidigits/tmp/pidigits.dart
null