Created
September 12, 2012 13:54
-
-
Save samklr/3706755 to your computer and use it in GitHub Desktop.
Random Stuff in Scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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