Project Euler #3 Ruby
<p>The task:</p>

<blockquote>
  <p>The prime factors of 13195 are 5, 7, 13 and 29.
  What is the largest prime factor of the number 600851475143?</p>
</blockquote>

<p>The correct answer is <code>6857</code>.</p>

<p>My code:</p>

<pre><code>def prime?(n)
  (2..(n-1)).each { |x| false if n % x == 0 }
  true
end

x = 2
prime_factor_arr = []
number = 600_851_475_143

while x &lt; number
  if number % x == 0 &amp;&amp; prime?(x)
    prime_factor_arr &lt;&lt; x
    number = number / x
  end
  x += 1
end 

puts prime_factor_arr.last
puts prime?(prime_factor_arr.last)
puts prime_factor_arr
</code></pre>

<p>In the above case, I get <code>1471</code> as the largest prime. If I change the code to:</p>

<pre><code>while x &lt; (number / x)
  if number % x == 0 &amp;&amp; prime?(x)
    prime_factor_arr &lt;&lt; x
  end
  x += 1
end 
</code></pre>

<p>I get <code>486847</code>. The array printed in the end is:</p>

<pre><code>[71, 839, 1471, 6857, 59569, 104441, 486847]
</code></pre>

<p>It is not clear to me why my code does not work. Could anybody help?</p>