def isPrime?(num)
i = 2
@isFalse = 0
while i < num
divisible = ((num % i) == 0)
if divisible
@isFalse += 1
return false
end
i += 1
end
true
end
def primes(max)
startTime = Time.now
(2..max).each do |p|
puts "#{p} #{isPrime?(p)}"
end
endTime = Time.now
puts "--------------------------------------------------"
puts " FALSE values in range from (2 thru #{max}): #{@isFalse} \n TRUE values in range from (2 thru #{max}): #{max-1-@isFalse}"
puts "\nTotal time to calculate is #{endTime - startTime} seconds!"
end
primes(10)
isPrime? checks if a given number is a prime number.
primes loads a range of numbers and checks if each is a prime.
I want to know how many numbers are prime within the range and how many aren't.
I added @isFalse += 1 thinking I can increment each time false is returned so that I can determine how many numbers in the range are false and use this to subtract from max to get how many numbers are true.
Entire code is working correctly except @isFalse is not properly incrementing. Why is that? Thanks for the help.
--UPDATE--
My Output: added puts "About to increment @isFalse" before @isFalse += 1
2 true
3 true
About to increment @isFalse
4 false
5 true
About to increment @isFalse
6 false
7 true
About to increment @isFalse
8 false
About to increment @isFalse
9 false
About to increment @isFalse
10 false
--------------------------------------------------
FALSE values in range from (2 thru 10): 1
TRUE values in range from (2 thru 10): 8
@isFalse += 1line (eg,puts "About to increment @isFalse"). This should help pinpoint the problem.puts "About to increment @isFalse"isPrime?is called,@isFalseis reset to0. So the result of@isFalsebeing 1 is from the last timeisPrime?was called (withnumequal to 10, incrementing@isFalseto 1).