Skip to content

Instantly share code, notes, and snippets.

@humandb
Created September 4, 2014 22:07
Show Gist options
  • Save humandb/4aa0b347f6354d420989 to your computer and use it in GitHub Desktop.
Save humandb/4aa0b347f6354d420989 to your computer and use it in GitHub Desktop.
def my_pow(x: Float, n: Int): Float = {
if (n == 0) {
1
} else {
/ **
* If you ommit this if it won´'t finishe
*/
if (n == 2) {
x * x
} else {
if (n > 0 & n % 2 == 1) {
x * my_pow(x, n - 1)
} else {
if (n > 2 & n % 2 == 0) {
my_pow(my_pow(x, n / 2), 2)
} else {
1 / my_pow(x, n)
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment