Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Random Stuff in Scala
def swap (x : Int; y : Int) = {
var a = x; var b =y;
a= a -b; b= b+a; a= b -a;
}
def fibonacci(n : Int) = recursiveFib(n, 1, 0)
def recursiveFib ( n : Int, next : Int, result : Int) : Int = {
n match {
case 0 => result
case _ => recursiveFib( n-1, next + result, next)
}
}
def streamFibonacci (n : Int) = nextFib(1,1).take(n).last
def nextFib(x: Int, y: Int): Stream[Int] = {
x #:: nextFib(y, x+y)
}
//#############Prime numbers and print the first 1000 ###########################
def isPrime = (2 until (math.sqrt(n).floor.toInt + 1)) forall (n % _ != 0)
(1 until 1000) filter(isPrime) foreach println
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.