The Computer Language
Benchmarks Game

pidigits Scala #3 program

source code

/* 
 * The Computer Language Benchmarks Game
 * http://benchmarksgame.alioth.debian.org/
 * contributed by Rex Kerr
 * based on version by John Nilsson as modified by Geoff Reedy
*/

object pidigits {
  type I = BigInt
  import BigInt._
    
  class LFT(q:I, r:I, t:I) {
    def compose(k: Int) = new LFT(q*k, (q*(4*k+2))+(r*(2*k+1)), t*(2*k+1))
    def extract = {
      val (y,rem) = (q*3 + r) /% t
      if((rem + q) < t) Some(y.intValue) else None
    }
    def next(y: Int) = new LFT(q*10, (r-(t*y))*10, t)
  }

  def pi_digits = {

/*  uses only ONE bigint division instead of TWO 

    def digits(z: LFT, k: Int): Stream[Int] = z extract match {
      case Some(y) => Stream.cons(y,digits(z next y,k))
      case None    => digits(z compose k,k+1)
    }
*/

    digits(new LFT(1,0,1),1)
  }

  def by[T](s: Stream[T], n: Int): Stream[Stream[T]] =
    if (s.isEmpty) Stream.empty
    else Stream.cons(s take n, by(s drop n, n))

  def main(args: Array[String]): Unit =
    for ((d,n) <- by(pi_digits take args(0).toInt, 10).zipWithIndex)
      printf("%-10s\t:%d\n",d.mkString,10*n+d.length)
}
    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
Scala compiler version 2.12.1 -- Copyright 2002-2016, LAMP/EPFL and Lightbend, Inc.
java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)


Mon, 06 Feb 2017 05:42:30 GMT

MAKE:
mv pidigits.scala-3.scala pidigits.scala
/usr/local/src/scala-2.12.1/bin/scalac -optimise -target:jvm-1.8 pidigits.scala
pidigits.scala:31: error: not found: value digits
    digits(new LFT(1,0,1),1)
    ^
one error found
/home/dunham/benchmarksgame/nanobench/makefiles/u64q.programs.Makefile:600: recipe for target 'pidigits.scala-3.scala_run' failed
make: [pidigits.scala-3.scala_run] Error 1 (ignored)
3.09s to complete and log all make actions

COMMAND LINE:
 /usr/local/src/jdk1.8.0_121/bin/java -Djava.library.path=Include/java -Xbootclasspath/a:/usr/local/src/scala-2.12.1/lib/scala-library.jar:/usr/local/src/scala-2.12.1/lib/scala-actors-2.11.0.jar:/usr/local/src/scala-2.12.1/lib/akka-actor_2.11-2.3.4.jar:/usr/local/src/scala-2.12.1/lib/config-1.2.1.jar pidigits 2000

PROGRAM FAILED 


PROGRAM OUTPUT:

Error: Could not find or load main class pidigits