SHARE
TWEET

Untitled

a guest Sep 11th, 2019 114 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top