Math algorithm written with Scala .