Skip to content

Instantly share code, notes, and snippets.

@toshia
Created January 2, 2012 16:57
Show Gist options
  • Save toshia/1551367 to your computer and use it in GitHub Desktop.
Save toshia/1551367 to your computer and use it in GitHub Desktop.
ある整数aを整数倍して別の整数b (a<b) に近づけるとき何倍すれば最もbに近い整数になるかがわかるアルゴリズムとは
# ある整数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