Skip to content

Instantly share code, notes, and snippets.

@pkukielka
Last active December 13, 2015 22:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pkukielka/4986750 to your computer and use it in GitHub Desktop.
Save pkukielka/4986750 to your computer and use it in GitHub Desktop.
import scala.actors.Futures._
object ScalaMul {
val cores = Runtime.getRuntime().availableProcessors()
val sets = (1 to cores).map(_ to 200000 by cores)
val jobs = sets.map(set => future(set.foldLeft(BigInt(1))(_ * _)))
val mult = awaitAll(3600000, jobs:_*).foldLeft(BigInt(1))(_ * _.get.asInstanceOf[BigInt])
def main(args: Array[String]) = println(mult)
}
// object ScalaMul {
// def mul(range: NumericRange[BigInt]): BigInt = {
// def half(f: Int => NumericRange[T]) = future(mul(f(range.length / 2)))
// if (range.length < 50000) range.foldLeft(BigInt(1))(_ * _)
// else awaitAll(3600 * 1000, half(range.take), half(range.drop).foldLeft(BigInt(1))(_ * _.get.asInstanceOf[BigInt])
// }
// def main(args: Array[String]) = println(mul((BigInt(1) to BigInt(200000))))
// }
// object ScalaMul {
// def mul(r: Range): BigInt = { var i = r.start; var sum: BigInt = 1; while (i <= r.end) { i += r.step; sum *= i }; return sum }
// val cores = Runtime.getRuntime().availableProcessors()
// val jobs = (1 to cores).map(_ to 1000000 by cores).map(range => future(mul(range)))
// def main(args: Array[String]) = println(awaitAll(1000000, jobs: _*).map(_.get.asInstanceOf[BigInt]).sum)
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment