Trouble Understanding Logic of Primes Program

I am reading a book called Beginning Java 7, by Ivor Horton. In his book he has a primes calculation program. The code is below. I am having trouble understanding the logic of the program. I've stared at it for the past several hours with no luck. What I can't understand is how both i and j are both initialized to 2 but the if statement:
if(i%j == 0) {
          continue OuterLoop;                                         
        }then passes a 2 to the println() function. I am totally lost on this problem. The program below outputs all of the prime numbers from 2 to 50 without any errors. Please help!
Thank you.
public class Primes2 {
  public static void main(String[] args) {
    int nValues = 50;                                                  // The maximum value to be checked
    // Check all values from 2 to nValues
    OuterLoop:
    for(int i = 2 ; i <= nValues ; ++i) {
      // Try dividing by all integers from 2 to i-1
      for(int j = 2 ;  j < i ; ++j) {
        if(i%j == 0) {                                                 // This is true if j divides exactly
          continue OuterLoop;                                          // so exit the loop
      // We only get here if we have a prime
      System.out.println(i);                                           // so output the value
}Edited by: EJP on 27/03/2013 19:54: fixed all the bizarre formatting, spelling, and removed the even more bizarre comment marketers around the text. Please use standard English and its existing conventions here.

Hi. I did notice that. What I don't understand is how 2 is outputted as a prime number when the Net Beans debugger shows that i's value is 3 in the if statement. Where does the value 2 come from? Thanks.
Edited by: 996440 on Mar 27, 2013 3:07 PM

Similar Messages

  • Have Trouble understanding the runnable interface

    Hi
    I am new to java programming. I have trouble understanding threading concepts.When I run the below code,Thread3 runs first. I need a small clarification here- I created two thread objects
    Thread thread1=new Thread(new RunnableThread(),"thread1");
    Thread thread2=new Thread(new RunnableThread(),"thread2");
    As soon as I create these objects -Will the constructor with the string argument in the "RunnableThread" class gets invoked. If so why doesn't System.out.println(runner.getName()); get invoked for the above objects. what is the sequence of execution for the below program.
    /*************CODE****************************/
    class RunnableThread implements Runnable
         Thread runner;
         public RunnableThread()
         public RunnableThread(String threadName)
              runner=new Thread(this,threadName);          //Create a new Thread.
              System.out.println(runner.getName());
              runner.start();          //Start Thread
         public void run()
              //Display info about this particular Thread
              System.out.println(Thread.currentThread());
    public class RunnableExample
         public static void main(String argv[])
              Thread thread1=new Thread(new RunnableThread(),"thread1");
              Thread thread2=new Thread(new RunnableThread(),"thread2");
              RunnableThread thread3=new RunnableThread("thread3");
              //start the threads
              thread1.start();
              thread2.start();
              try
                   //delay for one second
                   Thread.currentThread().sleep(1000);
              catch (InterruptedException e)
              //Display info about the main thread
              System.out.println(Thread.currentThread());
    }

    srinivasaditya wrote:
    Hi
    I am new to java programming. I have trouble understanding threading concepts.I'd consider it to be an advanced area.
    When I run the below code,Thread3 runs first. I need a small clarification here- I created two thread objects
    Thread thread1=new Thread(new RunnableThread(),"thread1");
    Thread thread2=new Thread(new RunnableThread(),"thread2");No, this is wrong. You don't need your RunnableThread. Runnable and Thread are enough. Your wrapper offers nothing of value.
    >
    As soon as I create these objects -Will the constructor with the string argument in the "RunnableThread" class gets invoked. If so why doesn't System.out.println(runner.getName()); get invoked for the above objects. what is the sequence of execution for the below program.did you run it and see? Whatever happens, that's the truth.
    %

  • Has the NVidia GeForce 8600M GT logic board replacement program been extended by a year?

    I have heard that the logic board replacement program for MacBooks Pro with the failed NVidia 8600M GT chip has been extended from 3 to 4 years after the date of original purchase. However, a local AASP had no knowledge of such an extension. Is this true? I have a MacBook Pro 17" 2.6GHz that was purchased in 2008.

    eww wrote:
    when you took it in, it had been more than four years since your 2007 machine was originally sold, that's why you got the response you did.
    Actually it was within the time frame, it felt like it was just last month, when it was like several months ago.
    Just dragged the 15" recently out to see to use it for testing *cough* and low and behold it's worthless now.
    The problem I have is I told them at the Genius Bar the video was going, but couldn't prove it as it was very rare it occured.
    But they ran their little tests and gave me a load of crap, now it's dead. $2500 down the toilet.
    I know what did it, Lion. Lion was the cause, it ran like a fine machine under Snow, fscking Lion, I'm glad I didn't put that POS on my new 17"
    http://reviews.cnet.com/8301-13727_7-20091204-263/graphics-issues-plaguing-os-x- 10.7-lion-upgrades/
    My word the OP, don't let the Genius Bar screw you with the same crap they gave me, call corporate and raise ****!

  • How do you connect your photoshop elements on your computer to your account online? and how do you create a customized url? how does the gallery work and how do you access it? i have trouble signing in on my program from my computer to connect to the onli

    how do you connect your photoshop elements on your computer to your account online? and how do you create a customized url? how does the gallery work and how do you access it? i have trouble signing in on my program from my computer to connect to the online photoshop, and I really want to create my own customized url and post photos to my gallery and share them with the world, family, and friends, but i need help because i can't figure how to do any of this, would really appreciate feedback and assistance, thanks, - claire conlon

    To add to sig's reply, "calibrating" does not calibrate Lithiu-Ion batteries, it calibrates the charge reporting circuitry.  If you look at the effect of deep discharging Lithium-Ion batteries in the data from the independent test group, Battery University, you will see that doing so shortens the life of the battery significantly. It looks like an optimum balance between use and life is at a discharge level of 50%.

  • I have downloades Logic Pro 9 onto my older modfel Mac Book Pro 1,1 with OS 10.6.8 and 2 GB RAM, according to the rquirements for Logic 9. the program should run, but it won't open. Is my configutation too slow?

    I have downloades Logic Pro 9 onto my older modfel Mac Book Pro 1,1 with OS 10.6.8 and 2 GB RAM, according to the rquirements for Logic 9. the program should run on OS 10.6.8. with 2 GB RAM, 4 GB better, but it won't open. Is my configutation too slow?

    Looks as if you're just at the minimum specs - http://www.apple.com/logicpro/specs/. Says 2GB minimum, 4GB recommended. And 2GB of RAM is all the RAM the 1,1 can handle. If I were you I would call Apple's App Store service center - you have the minimum requirements, as I read it, but perhaps you need a faster processor?
    I had a 1,1 but sold it when I bought my late 2011 in March of this year - maybe it's time to look at a new machine?
    Clinton

  • HT4623 I upgraded my iphone4S yesterday and Siri response with "having trouble understanding".  Is there an upgrade for Siri?

    I upgraded my iPhone4S to iOs6 and now Siri does not understand me.  Siri responses "having trouble understanding you, try again".  Is there an updgrade for Siri?  I have been searching all afternoon for a glimmer of a solution.  Any suggestions?

    Sure...
    See this Apple article for a Hard reset of a Factory reset.
    http://support.apple.com/kb/HT3728

  • What to know the logic of this program :RV60SBAT

    i need to change this program based on my requirement , but i don't know the logic of this program ,there are some puzzles about the logic of the program. the requirement is that: when create the batchjob, the number of the batchjob is fixed , and the steps of the batchjob is also need to fixed, but the per step that contain the number of the customer is dynamicly. how to do it .i need to fix the step of the batchjob ,how to do it ?
    it is very emergency . who can give me some useful suggestion?
    Thanks in advance.

    Hello, Ming.
    I'm not sure whether this will solve your problem, but check OSS note 652219. It describes an adjustment to the SDBILLDL program so that you can schedule it directly in background instead of it being "spawned" by the RV60SBAT program. That way, it can be a "fixed" scheduled job.
    Hope this helps!
    Cheers,
    Des.

  • Having trouble understanding Abstract class. Help!!!!!!

    Having trouble understanding Abstract class. when is Abstract class used and for what.

    Having trouble understanding Abstract class. when is
    Abstract class used and for what.An abstract class is used to force the developer to provide a subclass, to implement the abstract methods, while still keeping the methods that were provided.
    � {�                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • Trouble understanding KMP algorithm

    If anyone is familiar with the KMP skip search algorithm and cares to shed some light on the subject. I am having trouble understanding how the table is derived, as many examples i come across seem to be using different variations. The table used to control the search.
    Thanks a million in advance, Mel

    JosAH wrote:
    The failure function can be a bit of a burden to fully understand but realize that it gives you the next index of the string to search for if the current match for a prefix of the pattern failed. thats kind of what i wanted to hear lol, thanks both of you
    and i took ur advice, i did the whole example on pen and paper. i would be lieing if i said i fully understand it, but i do have a better understanding now.
    Mel

  • Trouble understanding static objects

    Hello,
    I have trouble understanding static objects.
    1)
    class TestA
    public static HashMap h = new HashMap();
    So if I add to TestA.h from within a Servlet, this is not a good idea, right?
    But if I just read from it, that is ok, right?
    2)
    class TestB
    public static SimpleDateFormat df = new SimpleDateFormat();
    What about TestB.df from within a Servlet? Is this ok?
    example: TestB.df.format(new Date(1980, 1, 20));

    There is exactly one instance of a static member for every instance of a class. It is okay to use static members in a servlet if they are final, or will not change. If they may change, it is a bad idea. Every call to a servlet launches a new thread. When there are multiple calls, there are multiple threads. If each of these threads are trying to make changes to the static memeber, or use the static memeber while changes are being made, the servelt could give incorrect results.
    I hope that helped..
    Thanks
    Cardwell

  • Require Logic for the Program

    I have a requirement  in which I need to write a program. I need to fetch the payer data which is having KNA1-ZZDTCESS value populated(Payer which are having this field value blank no need to fetch those payer).ZZDTCESS is the cession date and this is a custom field in KNA1.Its date element is DATUM.
    Now after fetching this KNA1-ZZDTCESS value of the payer this needs to be updated to the all the sold-to-party which are attached to this payer.Sold-to-party also having this custom field.
    Finally we need to develop a batch job which will run this program to udpate the ZZDTCESS value in the Sold-to-Party.
    Can you please let anyone know how to write the logic for this program?

    Hi ,
    Thanx for the answer but the requirement is there Sold to Party customer which is having different payer.For example see below.
    SP 1000
    SH 1000
    PY 2000
    BP 1000
    Now from the PY 2000 KNA1-ZZDTCESS value nees to be updated to the sold to party 1000 KNA1-ZZDTCESS  field.Please let me know how to write the logic for that.

  • How to transfer Logic Pro 9 program to another Mac

    HI there, I'm wanting to transfer my Logic Pro 9 program from my iMac to my Macbook Pro without losing content. Thanks in advance. J

    Hi, Jay.
    If you bought LP 9 from the App store you can download it again from Itunes.
    If is came on discs, then you must reinstall it. Using migration assistant will not always work.
    regards,
    Scorpii

  • Major trouble understanding structure

    Hello,
    This is my first time creating a Photoshop Plugin, and I am having a lot of trouble understanding how to do it. I am using Microsoft MFC to create it. Does anybody know of good references or sample code to look at? I simply don't get how to create this!!!
    I am trying to do a filter plugin that will let the user select a particular color, and turn all other colour in the image to black and white.
    Thanks.

    There is one MFC sample in the SDK. It doesn't sound like you are having trouble with MFC per say but more about getting pixels in and out of Photohsop. Where exactly are you having troubles? Maybe the Dissolve sample will help you out but it uses OS (Win32) API's and not MFC.

  • Trouble understanding programing question.

    Hey guys,
    Got a program im supposed to write its bonus and I have already done the original program. Problem is Ive read the instructions and they are kinda confusing to me. If I could just understand what they are saying then it will make this much easier. So if anyone can shed some light on what im missing id be thankful.
    Heres the program instructions.
    Question 2: Design and implement a recursive method that implements the factorial method n!. The factorial method n! is defined by
    n!= n*(n-1)*(n-2)*�*1
    Place the method in a class that has a main that tests the method.
    Does this just mean I need to make a method that finds the factors of a variable (e.g the factors of 10)? Which would be 2 and 5 right? Or would this mean make a recursive method that finds the factors of 10*n. like 1 * 10 = 10, 2 * 10 = 20, 3 * 10 = 30 etc etc.... And Im guessing that that algorightm cant be used in a program. In other words the *....*1 and n! are not correct syntax and that algorithm is just a abstract example to give me some direciton im not actually supposed to try and replicate it exactly as it is right? Usually I dont have problems with instructions but this one just confused the hell out of me. Any help would be great thank you. I would have asked the teacher but I thought I understood it and now I dont see the profeesor until the day its due and also she has a foreign accent so its hard for me to understand.
    Message was edited by:
    venture

    No. You have to do the factorial.
    3! = 3*2*1 = 6
    4! = 4*3*2*1 = 24
    etc
    You should see a pattern ie 4! = 4 * 3!. In the
    question you were given the equation for calculating
    it. If you can't do it Google is your friend becuase
    it has been done millions of times before.
    Slow! Well it is hard to type with these fins.
    Message was edited by:
    flounderThat makes sense thank you.

  • Opening Digital Performer files in Logic 8 -- translation programs?

    I have decades of Digital Performer files I want to be able to open in Logic 8 without going through a multi-step process (open in DP, save as midi, open in Logic).
    Is there a translation program, a method, or even a function within Logic that will streamline the opening of Digital Performer files? There used to be a program years ago that let me open various types of files in DP, so I'm wondering if there's a new equivilant for Logic. (I do realize there will be tweaking necessary after opening, but I just want to get them opened faster and within Logic -- right now they're grayed out, of course.)
    thx
    Neil

    NMcQ wrote:
    Yes I've looked through those and understand the long way of doing it, was just wondering if anyone knew of a translation 'shortcut' program, since I have so many to do.
    thx
    Neil
    http://www.solid-state-logic.com/post/pro-convert/index.asp
    Some people like it, some don't. If you need something like this, try it.
    Cheers
    PS I am not endorsing, selling, nor recommending whatever is on that link, moderator...

Maybe you are looking for

  • The Verizon "Experience"

    Hello, I ordered a phone this afternoon off the Verizon Wireless page and though it was close to the end of the business day, I chose to pick it up at "my store" (mostly because Verizon stores open generally at 10am, WAY too late to do any business o

  • Problem to import clips video with "Lightning to SD Card Camera Reader", my mini ipad and my camcorder?

    I need import immediatly my clips video from my camcorder in my mini ipad with my SD card. Solution: I have to use "Lightning to SD Card Camera Reader" Problem: When i insert my Sd card in the "Lightning to SD Card Camera Reader", my mini ipad don't

  • Call RFC from SharePoint

    Dear All I post this query to the forum after searching a lot to arrive a solution.  I need a tips / guidence from you guys. My requirement is as follows;  We have SAP R3 and SharePoint Portal.  In the Sharepoint portal the Purchase Requisitions are

  • Block change of Partner details in order

    Dear all, When we go to the Headers-->Partners tab in a sales order in the change mode, and double-click on the Partner mention there, a window opens wherein we can change the partner address and region. And thus make it different from that maintaine

  • Error rman-20021 while trying to backup dataabase

    Hi, I built a new instance 11g and tried to backup this database. My settings: RMAN> show all 2> ; RMAN-Konfigurationsparameter f³r Datenbank mit db_unique_name ORATOSHI sind: CONFIGURE RETENTION POLICY TO REDUNDANCY 1; # default CONFIGURE BACKUP OPT