Skip to content

Instantly share code, notes, and snippets.

@samklr
Created September 12, 2012 13:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samklr/3706755 to your computer and use it in GitHub Desktop.
Save samklr/3706755 to your computer and use it in GitHub Desktop.
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