Results 1 to 10 of 10
  1. #1
    Crossfires is offline Member
    Join Date
    Oct 2009
    Posts
    5
    Rep Power
    0

    Default Java primenumber application

    Hello everyone,

    I'm quite new to Java, i've got the assignment to make a class to show all the primenumbers from 1 to 100. I was stuck on the code and found some help online, i've got the class working working now though i don't understand one part from the application. Can anyone explain?

    public class Primenumbers {

    public static void main(String args[]) {
    int number;

    //Startwaarde a = 0 en 100 eindwaarde
    for (number = 2; number <= 100; number++) {
    boolean prime = true;

    for (int divider = 2; divider <= divider/2; divider++)
    {
    if (number % divider == 0)
    {
    priem = false;
    }
    }



    if (priem)
    {
    System.out.println(number + " is a primenumberl");
    }
    }
    }
    }

    int divider = 2; divider <= divider/2; divider++ is the part i don't understand. Why does it say divider/2; ? Please keep in mind i'm a beginner so be gentle with your explaination.

    Thanks!

  2. #2
    Vio
    Vio is offline Member
    Join Date
    Oct 2009
    Location
    Belgium
    Posts
    18
    Rep Power
    0

    Default

    I think "divider/2" is wrong and should be "number/2", maybe you'll understand now.

  3. #3
    Crossfires is offline Member
    Join Date
    Oct 2009
    Posts
    5
    Rep Power
    0

    Default

    Correct it should be number / 2 but that is my typo. It should say that but i still don't understand.

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    1.) Please use code tags when posting code.
    2.)That's the problem of ripping codes off the internet and passing it in as your own.
    Better try to write it on your own next time.
    3.)Run that loop manually writting values of all the relevant variables in a table to see what they are at each stage of the loop. That way you will understand the code more.

    P.S You probably didn't post the exact code you've got too.

  5. #5
    Vio
    Vio is offline Member
    Join Date
    Oct 2009
    Location
    Belgium
    Posts
    18
    Rep Power
    0

    Default

    To explain it simply I'll use an example. For example you reach number = 18.
    Then why should you check if it's dividable by 17,16,15,14,13,12,11,10 ? You are sure 18 is not dividable by them, so you only have to check the numbers from 18/2 = 9 till 2. And this is correct for every number.

  6. #6
    Crossfires is offline Member
    Join Date
    Oct 2009
    Posts
    5
    Rep Power
    0

    Default

    Thank you for you explaination, this is what i thought. Though if, for some reason you are inefficient, how would that line of the code look if you would just check devisions 17, 16 etc. as well. If i try to change the code to do this like change number /2 to just number, it won't work. I don't understand why

  7. #7
    Vio
    Vio is offline Member
    Join Date
    Oct 2009
    Location
    Belgium
    Posts
    18
    Rep Power
    0

    Default

    And what goes wrong then? I guess you get no prime numbers then?

  8. #8
    Crossfires is offline Member
    Join Date
    Oct 2009
    Posts
    5
    Rep Power
    0

    Default

    Yes, it wont work

  9. #9
    Crossfires is offline Member
    Join Date
    Oct 2009
    Posts
    5
    Rep Power
    0

    Default

    I now understand why that won't work because if the number is divided by itself you will have module 0 and the prime will be false (even if it's not.) Thank you so much for your help.

  10. #10
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    not sure if this was just a typo but your variable is declared as prime and when checking you have it spelled priem
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

Similar Threads

  1. how to call Java Bean Ireport frm Java application
    By Prashant.surwade in forum Advanced Java
    Replies: 8
    Last Post: 05-06-2012, 03:39 PM
  2. Log on for GUI java application
    By VinTiger in forum New To Java
    Replies: 0
    Last Post: 03-29-2009, 07:37 PM
  3. Java EE Web Application
    By dan0 in forum Advanced Java
    Replies: 1
    Last Post: 03-25-2009, 09:45 PM
  4. java application
    By jithan in forum New To Java
    Replies: 4
    Last Post: 06-30-2008, 04:30 PM
  5. Using Java in SWF Application
    By ajayk in forum SWT / JFace
    Replies: 0
    Last Post: 06-25-2008, 08:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •