Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- object Bud {
- def buddy(start: Long, limit: Long): String = {
- case class Memo[A,B](f: A => B) extends (A => B) {
- import scala.collection.mutable
- private val cache = mutable.Map.empty[A, B]
- def apply(x: A):B = cache.getOrElseUpdate(x, f(x))
- }
- def s(n: Long): Long = {
- (1L to (math.sqrt(n)+0.5).toLong).view.filter(n % _ == 0).flatMap(x => Seq(x, n/x)).distinct.sum - n
- }
- lazy val f = Memo[Long, Long](s)
- (start to limit).find(n => f(n)>n+1 && n== f(f(n)-1) - 1) match {
- case Some(x) =>
- s"($x ${f(x)-1})"
- case None => "Nothing"
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement