Created
August 6, 2014 18:18
-
-
Save jayunit100/b294c8eae07b07a8c7c0 to your computer and use it in GitHub Desktop.
is there a more scala-ish way to do this?
This file contains hidden or 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 pow(n:Int , k:Int ) : Int = { | |
k match { | |
case 0 => return 1; | |
case 1 => return n; | |
case 2 => return n*n; | |
case _ => ; | |
} | |
if ( (k%2)==0) { | |
var p = | |
pow(n,k/2) * | |
pow(n,(k+1)/2); | |
return p; | |
} | |
if (k%2==1) { | |
var p = pow(n,k/2) | |
return p * p; | |
} | |
0 | |
} | |
def x : Int = 2; | |
def y : Int = 4; | |
println(pow(x,y)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment