Created
January 2, 2012 16:57
-
-
Save toshia/1551367 to your computer and use it in GitHub Desktop.
ある整数aを整数倍して別の整数b (a<b) に近づけるとき何倍すれば最もbに近い整数になるかがわかるアルゴリズムとは
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
# ある整数aを整数倍して別の整数b (a<b) に近づけるとき何倍すれば最もbに近い整数になるかがわかるアルゴリズムとは | |
def expect(expect, result) | |
expect == result end | |
def are(a, b) | |
c = b / a # => 3, 7, 2 | |
(a*c - b).abs > (a*(c+1) - b).abs ? c+1 : c # => 3, 7, 3 | |
end | |
expect(3, are(2, 6)) # => true | |
expect(7, are(3, 22)) # => true | |
expect(3, are(5, 14)) # => true | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment