Created
February 3, 2014 08:14
-
-
Save svs/8780376 to your computer and use it in GitHub Desktop.
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
# why not? | |
def power(n) | |
2 ** n | |
end | |
# if you must do it the brute force way | |
def power(n) | |
return 1 if n == 0 | |
(1..[0,n-1].max).reduce(2) { |acc, n| acc * 2 } | |
end | |
# one could also write it recursively, but the above two would be most ruby-ish | |
# check http://ruby-doc.org/core-2.1.0/Enumerable.html#method-i-reduce |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment