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!