Note that there are some explanatory texts on larger screens.

plurals
  1. POSyntax error? Ruby bug? Issue with instance variable incrementing with += 1?
    primarykey
    data
    text
    <pre><code>def isPrime?(num) i = 2 @isFalse = 0 while i &lt; 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) </code></pre> <p><code>isPrime?</code> checks if a given number is a prime number. <code>primes</code> loads a range of numbers and checks if each is a prime.</p> <p>I want to know how many numbers are prime within the range and how many aren't.</p> <p>I added <code>@isFalse += 1</code> 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 <code>max</code> to get how many numbers are true.</p> <p>Entire code is working correctly except <code>@isFalse</code> is not properly incrementing. Why is that? Thanks for the help.</p> <p><strong>--UPDATE--</strong><br> My Output: added <code>puts "About to increment @isFalse"</code> before <code>@isFalse += 1</code></p> <pre><code>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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload