Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

158
Views
I am trying to write a code that checks if a number between 2 and 1000 is a prime Number
import java.util.Scanner;

public class Main {

  public static void main(String[] args) {
  
    for(int j = 2; j<=1000; j++) {
        boolean yes = true;
        for(int i = 2; i<j && yes== true; i++){
            if(j%i==0) {
                yes=false;
            }
        System.out.println(j + ":" + yes); 
      } 
    }  
  }
}      

I am trying to understand where is the problem without any answer so far.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You need to move System.out.println(j + ":" + yes); out of the inner loop. The reason why you need to move it out is that if the number is prime or not is decided only after the inner loop is finished.

public class Main {
    public static void main(String[] args) {
        for (int j = 2; j <= 1000; j++) {
            boolean yes = true;
            for (int i = 2; i < j && yes == true; i++) {
                if (j % i == 0) {
                    yes = false;
                }
            }
            if (yes) {
                System.out.println(j + ":" + yes);
            }
        }
    }
}

Side note: You are not required to check up to i < j. It can be i <= Math.sqrt(j). Check https://en.wikipedia.org/wiki/Primality_test to learn more about it.

Also, if you want to print the false as well, do not use the if (yes) {} block.

public class Main {
    public static void main(String[] args) {
        for (int j = 2; j <= 1000; j++) {
            boolean yes = true;
            for (int i = 2, n = (int) Math.sqrt(j); i <= n && yes == true; i++) {
                if (j % i == 0) {
                    yes = false;
                }
            }

            System.out.println(j + ":" + yes);

        }
    }
}
over 4 years ago · Santiago Trujillo Report

0

This can be done using Streams as follows:

IntStream.range(2, 1001)
         .boxed()
         .filter(j -> IntStream.range(2, j).boxed().allMatch(i -> j % i != 0))
         .forEach(System.out::println);

First, we:

  • generate the indices between 2 to 1000 (inclusive) i.e., IntStream.range(2, 1001),

  • then we convert from IntStream into Integer (i.e., boxed()),

  • then we filter to only the number that are prime

    .filter(j -> IntStream.range(2, j).boxed().allMatch(i -> j % i != 0)),

  • finally we print out the result.

This kind of pipeline is less prone to the mistakes that you have done in your original code.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!