Skip to content

Instantly share code, notes, and snippets.

@jackliusr
Created August 7, 2014 15:07
Show Gist options
  • Save jackliusr/486708eaf9193a8339fb to your computer and use it in GitHub Desktop.
Save jackliusr/486708eaf9193a8339fb to your computer and use it in GitHub Desktop.
Hackerrank Is Fibo in scala
import scala.math.BigInt
object Solution {
def main(args: Array[String]) = {
def fibFrom(a: BigInt, b: BigInt): Stream[BigInt] = a #:: fibFrom(b, a + b)
val lines = io.Source.stdin.getLines.drop(1).toList
val nums:List[BigInt] = lines.map( l => BigInt(l))
def isFib(n: BigInt):String = {
val fs = fibFrom(0,1).takeWhile(f => f <= n).toList
if (fs.last < n ) "IsNotFibo" else "IsFibo"
}
nums.foreach( n => println(isFib(n)))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment