Commit e8d4f78a by HankG

### Merge branch '10-straight-port-pidigits' into 'develop'

```Straight port of pidigits

Closes #10

See merge request !7```
parents fa40a823 edbfc89b
 /* The Computer Language Benchmarks Game http://benchmarksgame.alioth.debian.org/ Based on Java version (1) by Isaac Gouy Contributed by Hank Grabowski */ import java.math.BigInteger object pidigits { internal val L = 10 @JvmStatic fun main(args: Array) { var n = Integer.parseInt(args[0]) var j = 0 val digits = PiDigitSpigot() while (n > 0) { if (n >= L) { for (i in 0..L - 1) print(digits.next()) j += L } else { for (i in 0..n - 1) print(digits.next()) for (i in n..L - 1) print(" ") j += n } print("\t:") println(j) n -= L } } } internal class PiDigitSpigot { var z: Transformation var x: Transformation var inverse: Transformation init { z = Transformation(1, 0, 0, 1) x = Transformation(0, 0, 0, 0) inverse = Transformation(0, 0, 0, 0) } operator fun next(): Int { val y = digit() if (isSafe(y)) { z = produce(y) return y } else { z = consume(x.next()) return next() } } fun digit(): Int { return z.extract(3) } fun isSafe(digit: Int): Boolean { return digit == z.extract(4) } fun produce(i: Int): Transformation { return inverse.qrst(10, -10 * i, 0, 1).compose(z) } fun consume(a: Transformation): Transformation { return z.compose(a) } } internal class Transformation { var q: BigInteger var r: BigInteger var s: BigInteger var t: BigInteger var k: Int = 0 constructor(q: Int, r: Int, s: Int, t: Int) { this.q = BigInteger.valueOf(q.toLong()) this.r = BigInteger.valueOf(r.toLong()) this.s = BigInteger.valueOf(s.toLong()) this.t = BigInteger.valueOf(t.toLong()) k = 0 } constructor(q: BigInteger, r: BigInteger, s: BigInteger, t: BigInteger) { this.q = q this.r = r this.s = s this.t = t k = 0 } operator fun next(): Transformation { k++ q = BigInteger.valueOf(k.toLong()) r = BigInteger.valueOf((4 * k + 2).toLong()) s = BigInteger.valueOf(0) t = BigInteger.valueOf((2 * k + 1).toLong()) return this } fun extract(j: Int): Int { val bigj = BigInteger.valueOf(j.toLong()) val numerator = q.multiply(bigj).add(r) val denominator = s.multiply(bigj).add(t) return numerator.divide(denominator).toInt() } fun qrst(q: Int, r: Int, s: Int, t: Int): Transformation { this.q = BigInteger.valueOf(q.toLong()) this.r = BigInteger.valueOf(r.toLong()) this.s = BigInteger.valueOf(s.toLong()) this.t = BigInteger.valueOf(t.toLong()) k = 0 return this } fun compose(a: Transformation): Transformation { return Transformation( q.multiply(a.q), q.multiply(a.r).add(r.multiply(a.t)), s.multiply(a.q).add(t.multiply(a.s)), s.multiply(a.r).add(t.multiply(a.t)) ) } }
 ... ... @@ -72,6 +72,7 @@ onlydirs = fannkuchredux knucleotide mandelbrot pidigits ... ... @@ -130,6 +131,7 @@ make = nbody = 10000 20000 30000 40000 50000 regexdna = 10000 binarytrees = 1 2 5 10 15 20 pidigits = 1 100 1000 2500 5000 10000 ... ...