Last active
December 16, 2015 07:58
-
-
Save yancya/5402387 to your computer and use it in GitHub Desktop.
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
# 1 から n までの連続数を足す関数 | |
def sum_one_to(n) | |
(1+n)*(n/2)+(1+n)/2*(n%2) | |
end | |
# ↑のほうが↓よりも圧倒的に高速 | |
def sum_one_to(n) | |
(1..n).inject(0){|m,n| m+=n} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment