Reputation: 153
The following returns a Type Error in my conditional within the do block Range can't be coerced into Integer
def coprime?(num_1, num_2)
min = [num_1, num_2].min
max = [num_1, num_2].max
[2..min].each do |ele|
if max % ele == 0
return true
end
end
return false
end
p coprime?(25, 12) # => true
p coprime?(7, 11) # => true
p coprime?(30, 9) # => false
p coprime?(6, 24) # => false
Upvotes: 0
Views: 833
Reputation: 841
2..min
is a range, therefore [2..min]
is actually an array with 1 element and the ele
will be 2..min
It should be (2..min).each do |ele|
instead of [2..min].each do |ele|
Also you don't need to return false
at the end just false
is enough.
Upvotes: 1
Reputation: 33420
Because ranges in Ruby should have the form (start..end)
. In your case [2..min]
is an array with only one element, where that element is a range.
You can replace the []
with ()
to create a range from 2 to min
:
(2..min).each do |ele|
if max % ele == 0
return true
end
end
Upvotes: 3