Recursive, functional approach using lazy streams in Scala:
import scala.annotation.tailrec def calculate(m: Long): Int = { val cubes = Stream.from(1).map(_.toLong).map(i => i * i * i) def sumOfFirstXCubes(x: Int): Long = cubes.take(x).sum @tailrec def rec(i: Int, predicate: Long => Boolean): (Int, Long) = { val t = sumOfFirstXCubes(i) if (predicate(t)) rec(i + 1, predicate) else (i, t) } rec(1, _ < m) match { case (i, x) if x == m => i case _ => 0 } }
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Recursive, functional approach using lazy streams in Scala: