Reputation: 873
Hopefully something less newbish than my last...
Anyhow, I am doing a bit of coding on a small app that simplifies numbers down to primes, mainly to help with small things like homework.
However, a particular method is giving me the error mentioned in the title:
def get_simps(num)
curr = 2
print("Working...")
while (curr <= num)
#If they divide cleanly, then it's a simplified form
if (num % curr == 0)
res = [curr, num / curr]
break
end
curr += 1
end
print("\n")
return res
end
Where the argument num is supplied by this statement:
print("Insert number here: ")
num = gets().chomp().to_i()
Thus making the error weird: why does it say I compare a Fixnum and an ARRAY? I also did this:
if (num.class() == curr.class())
print "Cheese"
end
and it printed Cheese. Why the reason for the error, then?
Upvotes: 0
Views: 2452
Reputation: 52316
The code as published doesn't look like it should give the error described unless you inadvertently feed it an array.
You might want to look at the divmod() function, which could clean up the inner loop somewhat. And you're going to perform a lot of unnecessary integer divisions should your smallest prime factor be large.
It's not the answer you're looking for, but a particularly elegant Ruby prime factor solution can be found here
Upvotes: 1