This is my code thus far:
"def is_prime(num)
i = 2
while i < num.to_i
is_divisible = ((num % 1) == 0)
if is_divisible
return false
end
i += 1
end
true
end
def prime(max)
primes_arr = []
q = 2
while q < max.to_i
if is_prime(q)
primes_arr << q
end
q += 1
end
primes_arr
end
class Object
def is_number?
self.to_i.to_s == self.to_s || self.to_f.to_s == self.to_s
end
end
puts 'This is a program that first determines whether a number is prime
or not, ’
puts ‘then displays an array of all prime numbers less than your given
number.’
puts ‘Part 1: Please enter a natural number greater than 1.’
num = gets.chomp
while num.to_i <= 1
puts ‘That’s not a natural number greater than 1, silly. Try again:’
num = gets.chomp
end
puts 'That your number is prime is ’ + is_prime(num).to_s + ‘.’
puts ‘’
puts ‘Part 2: Please enter a max number.’
max = gets.chomp
while max.is_number? == false
puts ‘That’s not a number, silly.’
puts ‘Try again.’
max = gets.chomp
end
puts ‘’
puts ‘The prime numbers smaller than your max are: ’ +
prime(max).join(’, ') + ‘.’"
The first bit works, but the second bit (viz. the part that spits out
all prime numbers smaller than your “max”) always outputs (assuming one
has entered a number) either nothing, or 2.
Can anyone provide any insight as to why that is so? Thanks in advance
:]