def divisible?(n)
if n % 1 == 0 &&
n % 2 == 0 &&
n % 3 == 0 &&
n % 4 == 0 &&
n % 5 == 0 &&
n % 6 == 0 &&
n % 7 == 0 &&
n % 8 == 0 &&
n % 9 == 0 &&
n % 10 == 0 &&
n % 11 == 0 &&
n % 12 == 0 &&
n % 13 == 0 &&
n % 14 == 0 &&
n % 15 == 0 &&
n % 16 == 0 &&
n % 17 == 0 &&
n % 18 == 0 &&
n % 19 == 0 &&
n % 20 == 0
return true
else
return false
end
end
Tell me more
×
Code Review Stack Exchange is a question and answer site for
peer programmer code reviews. It's 100% free, no registration required.
|
|
|||
|
|
|
Ruby refactor: use Enumerable#all?:
Mathematical refactor:calculate the least common multiple of the integers in the range (Integer#lcm is available from Ruby 1.9):
This second snippet is, of course, more efficient once you pre-calculate |
|||||||||
|
|
Note that you're doing this, which is a pretty egrigious use of if/else:
If the branches of your if/else are Ignoring the ability to clean up your condition itself, a first pass at cleaning up your function could be:
|
|||
|
|