I am trying to find the factors of a number N. I want to be able to iterate over my prime numbers array, and when a condition is met, go back to start of the iteration, rather than just continuing on.
arrFactors = []
N = 150
[2,3,5,7,11].each do |a|
if N % a == 0
arrFactors.push(a)
N = N/a
break if N == 1
<return to start of the collection [2,3,5,7,11]>
end
end
How can I do this in Ruby 1.8.6? (I notice there is a "cycle" enumerator in 1.8.7 that maybe relavent?)
P.S
I know the below algorithm is not perfect yet (I think I need to provide for N = 1). I also know Ruby provides a factoring method, but I don't want to use 'cause I'm learning here.