
Untitled
By: a guest on
Aug 7th, 2012 | syntax:
None | size: 0.78 KB | hits: 6 | expires: Never
How can i test if a value is a prime number in ruby?? the easy way?? and the hard way?
class DetermineIfPrime
def initialize (nth_value)
@nth_value = nth_value
primetest
end
def primetest
if Prime.prime?(@nth_value)
puts ("#{@nth_value} is prime")
else
puts ("This is not a prime number.")
end
rescue Exception
puts ("#{$!.class}")
puts ("#{$!}")
end
end
NameError
uninitialized constant DetermineIfPrime::Prime
class DetermineIfPrime
def initialize (nth_value)
@nth_value = nth_value
primetest
end
def primetest
for test_value in [2, 3, 5, 7, 9, 11, 13] do
if (@nth_value % test_value) == 0
puts ("#{@nth_value} is not divisible by #{test_value}")
else
puts ("This is not a prime number since this is divisible by #{test_value}")
break
end
end
end
end