title: Ruby each vs while loop performance
body: <p>trying to resolve a basic problem of algorithm in Ruby, and testing performance. </p>
  
  <p>Just in case, the algorithm aims to find the smallest positive number that is evenly divisible by all of the numbers from 1 to 20.
  Here's the code : </p>
  
  <pre><code>def remainder(number) # with while
    divisor = 2
    while divisor &lt; 21
      return false unless number % divisor == 0
      divisor += 1
    end
    true
  end
  
  def remainder(number) # with each
    (2..20).each do |divisor|
      return false unless number % divisor == 0
    end
    true
  end
  
  number = 180_000_000
  while number &lt; 10_000_000_000
    if remainder number
      puts "#{number}"
      break
    end
    number += 1
  end
  </code></pre>
  
  <p>On my computer, with while version, Ruby takes around 10 seconds, with each version, it takes between 70 and 80 seconds to resolve. Code does the exact same thing, gives same result. Why such a difference in performance ?</p>