Skip to content

Instantly share code, notes, and snippets.

@yancya
Last active December 16, 2015 07:58
Show Gist options
  • Save yancya/5402387 to your computer and use it in GitHub Desktop.
Save yancya/5402387 to your computer and use it in GitHub Desktop.
# 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