Advertisement
Guest User

Untitled

a guest
Sep 11th, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.61 KB | None | 0 0
  1. object Bud {
  2. def buddy(start: Long, limit: Long): String = {
  3.     case class Memo[A,B](f: A => B) extends (A => B) {
  4.       import scala.collection.mutable
  5.       private val cache = mutable.Map.empty[A, B]
  6.       def apply(x: A):B = cache.getOrElseUpdate(x, f(x))
  7.     }
  8.  
  9.     def s(n: Long): Long = {
  10.       (1L to (math.sqrt(n)+0.5).toLong).view.filter(n % _ == 0).flatMap(x => Seq(x, n/x)).distinct.sum - n
  11.     }
  12.  
  13.     lazy val f = Memo[Long, Long](s)
  14.  
  15.     (start to limit).find(n => f(n)>n+1 && n== f(f(n)-1) - 1) match {
  16.       case Some(x) =>
  17.         s"($x ${f(x)-1})"
  18.       case None => "Nothing"
  19.     }
  20.   }
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement