public
Last active

Farmer's weights

  • Download Gist
farmers-weights.scala
Scala
1 2 3 4 5 6 7 8 9 10
import scala.math._
 
def stone_weights(w: Int) : List[Int] = w match {
case 1 => List(1)
case _ =>
val one_third : Int = ceil((w-1).toDouble/3).toInt
w - one_third :: stone_weights (one_third)
}
 
println(stone_weights(40));

Very elegant, awesome!

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.