This program is supposed to print out prime numbers from 1 to 50:

public class  Break{
  public static void main(String[] args){
    int i,j;
    System.out.println("Prime numbers between 1 to 50 : ");
    for (i = 1;i < 50;i++ ){
      for (j = 2;j < i;j++ ){
        if(i % j == 0)
        {
          break;
        }
      }
      if(i == j)
      {
        System.out.print("  " + i);
      }
    }
  }
}

But why do i have to declare two variables j and i and the get the remainder, and also create loops for each one of them can someone please explain?

Outer loop tests all the numbers from 1 to 50 (i)
For each i the inner loop tries dividing it by all the numbers less than i (j)
If the remainder is 0 then i is exactly divisible by j , so i cannot be prime and it breaks out of the inner loop.
After exiting the inner loop if j==i then it has tested every possible divisor, and so i must be prime.

To test whether you understand the code after reading James' explanation, there's a very simple change you can make to this which will eliminate about half of the work this program has to do. The change is very simple, it involves one arithmetic operation inserted in the right place.
Can you spot it?

kiparsky, if you do the same arithmetic to both loops, you can reduce the work by this program has to do by 3/4.

Hey i do not get what it means to do same arithmatic in both loops..??????
I understand the logic...its correct...

yes pooja.shinde. James' explanation is correct, but you can make your code more efficient as Kiparsky said. try as an exercise to make your program more efficient. if you make the program more efficient, you will be sure you understand the logic. try first finding the prime numbers between 1 and 50 by hand, using your algorithm, and youll see for sure what we mean.

I don't understand that Bibiki said either :-(

James, I'm taking my chance to teach you something this time. although last time you deleted the entire contents of a file of mine with that code you sent me. anyways :P

I'll copy paste KIparsky's message:
"To test whether you understand the code after reading James' explanation, there's a very simple change you can make to this which will eliminate about half of the work this program has to do. The change is very simple, it involves one arithmetic operation inserted in the right place.
Can you spot it?"

what I am saying is that you can actually eliminate 3/4 of the work that the following code does:

public class Break{

      public static void main(String[] args){

      int i,j;

      System.out.println("Prime numbers between 1 to 50 : ");

      for (i = 1;i < 50;i++ ){

      for (j = 2;j < i;j++ ){

      if(i % j == 0)

      {

      break;

      }

      }

      if(i == j)

      {

      System.out.print(" " + i);

      }

      }

      }

      }

see what I'm saying this time James?

Don't tell me you selected an existing file as the output file for my little demo? Surely not. Good thing you have proper backups.

Anyway, so, sorry, I fully understand K's post but I still don't understand your "do the same arithmetic to both loops". If I read that literally you will only look for primes up to 25.

OK, clarified by B in a PM - he's right, although I suspect he's thinking of a different method than K was (?).

I suspect he's thinking of a different method than K was

I think so, too. I don't want to make it too complicated. I'm guessing he's talking about making it into a proper Sieve, which of course is more efficient, but conceptually requires another step. I like to go one thing at a time. So, for anyone interested, I think bibiki's advice to work the original code by hand is excellent advice. I'm pretty sure the simplest fix you could do will jump right out at you - you'll see that there's a section of the work that this program does which is unnecessary, and is easily avoided.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.