Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You made 2 mistakes. The first is explained in the comment by <a href="https://stackoverflow.com/a/16726622/624041">@Thomas</a> and the second is explained in the comment by <a href="https://stackoverflow.com/a/16726635/624041">@rolfl</a>. I corrected them both here:</p> <pre><code>public class PrimeNumber extends ConsoleProgram { public void run() { for (int i = 50; i &gt;= 2; i--) { boolean isPrime = true; for (int j = 2; j &lt; i-1; j++) { //increment "j" not "i" if (i % j == 0) { isPrime = false; break; } } if (isPrime) System.out.println(i); } } } </code></pre> <p>Note: I verified the solution using this code (save in your IDE as PrimeNumber.java):</p> <pre><code>public class PrimeNumber { public static void main (String args[]) { for (int i = 50; i &gt;= 2; i--) { boolean isPrime = true; for (int j = 2; j &lt; i-1; j++) { //increment "j" not "i" if (i % j == 0) { isPrime = false; break; } } if (isPrime) System.out.println(i); } } } </code></pre> <hr> <p><strong>Edit:</strong> For your understanding, your main problem is the logic here:</p> <pre><code>for (int j= 2; j &lt; i-1; j++) { if (i % j &gt;= 1) { println(i); </code></pre> <p>You are printing <code>i</code> after only checking one possibility. </p> <p>For example, take <code>i = 7</code>. You have to test <code>i % j</code> for <code>j</code> = 6, 5, 4, 3, and 2 before you can say that <code>i</code> is prime. You can't just test <code>i % j</code> for <code>j = 6</code>, as you have done. To do so, your <code>println</code> statement should come after the for loop, not nested inside of it, so you can test all the possible values of <code>j</code> first.</p> <hr> <p><strong>Edit 2:</strong> In response to</p> <blockquote> <p>Coincidentally enough, the 1st part of the assignment is to write a predicate method that returns true if the number is prime and false if it is not prime using the strategy of brute-force. The 2nd part of the assignment is to find a more efficient way and reworking the 1st part of the assignment to be more efficient. I was trying to solve the problem using just using two for loops to see if i could do it. Can it be done with just two for loops w/o label and continue since my book has not covered that yet?</p> </blockquote> <p>Try something like this:</p> <pre><code>public boolean isPrime(int number) { for (int i = 2; i &lt; number; i++) { if (number % i == 0) { return false; //not prime } } return true; //prime } </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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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