Stack level too deep (SystemStackError)

When writing a code to find the factor of a given number, i ran into the
following error message:

“factor.rb:48: parse error, unexpected kEND, expecting $”

I was hoping someone could shed light on the problem. Heres the code:

def isprime testNum
testWith = 2
run = 1
while testNum > testWith and run == 1
prime = testNum % testWith
if prime != 0
testWith = testWith + 1
else
run = 2
end
end

if prime != 0
	return true
else
	return false
end

end

def factor toFactor
factored = false
numbers = Array.new
numbers.push toFactor
testWith = 2
while factored == false and numbers.last > testWith
test = numbers.last
isFactor = numbers.last%testWith
if isFactor == 0
numbers.pop
numbers.push(testWith)
numbers.push(test/testWith)
factored = true
else
testWith = testWith + 1
end
end

eval = numbers.last
	if isprime(eval) == false
		numbers.pop
		add = factor(eval)
		numbers.push(add)
	end
end

numbers

end

f = factor(100)

puts f.join(’ x ')


Note: subsituting " eval = numbers.last" for "numbers.each do |eval|
"avoids the error.

Please Help!

john s wrote:

  run = 1
  return true

testWith = 2
end
numbers

Please Help!

I don’t think Array#last takes a block.

-Justin

    eval = numbers.last
            if isprime(eval) == false  #
                    numbers.pop          #
                    add = factor(eval)   #unindent this bit
                    numbers.push(add) #
            end                                  #
    end # get rid of this.

then it works.
-tim

The last bit could be changed to:
print f.map{|x|x.is_a?(Array) ? “(#{x.join(” x “)})” : x}.join(" x ")

To add the correct brackets.

J`ey
http://www.eachmapinject.com