Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By christopherx

Thread: for(d=1;d<1000;d++)for(c=1;c<d;c++)d%c!=0; what is wrong

  1. #1
    jihad is offline Member
    Join Date
    Oct 2011
    Posts
    24
    Rep Power
    0

    Default for(d=1;d<1000;d++)for(c=1;c<d;c++)d%c!=0; what is wrong

    for(d=1;d<1000;d++)for(c=2;c<d;c++)d%c!=0;
    what is wrong
    Write an application that prints all prime numbers between 2 and 1000.that question is my hw
    I use "for" wrongly but I couldnt find
    it gaves eror that is "not a statement"
    Last edited by jihad; 10-30-2011 at 12:54 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: for(d=1;d<1000;d++)for(c=1;c<d;c++)d%c!=0; what is wrong

    For one thing you are not using whitespace appropriately. Place statements on their own lines, indent appropriately, and use curly brackets {} for all blocks including one line for loop blocks.
    Last edited by Fubarable; 10-30-2011 at 01:28 PM.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default Re: for(d=1;d<1000;d++)for(c=1;c<d;c++)d%c!=0; what is wrong

    d%c != 0 isn't a statement; the compiler is right (as always).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    christopherx is offline Member
    Join Date
    Oct 2011
    Posts
    92
    Rep Power
    0

    Default Re: for(d=1;d<1000;d++)for(c=1;c<d;c++)d%c!=0; what is wrong

    I can see your thought process mate. Your code is, frankly, poorly structured but your reasoning is alright.

    You've basically said that if a number, x, is not a multiple of any value between x - 1 and 0, then it must be a prime.
    The problem is the code you've written doesn't reflect that thought path. and ! is not the mathematical symbol for factorial in Java, it is the logical symbol for NOT. On a side note, even if it was the mathematical symbol for factorial this doesn't work. You want x-1, x-2, x-3.. not (x * x - 1), (x * x - 2).. What you need too do, is loop through and test if, for each value of c, in decrements of 1, are multiples of d. If you get to 0 and no multiple has been found, then you have a prime. by the way, there are FAR more efficient ways of testing for primes, although they never yield mathematical certainty like this does :)
    Fubarable likes this.

  5. #5
    jihad is offline Member
    Join Date
    Oct 2011
    Posts
    24
    Rep Power
    0

    Default Re: for(d=1;d<1000;d++)for(c=1;c<d;c++)d%c!=0; what is wrong

    thanks alot

  6. #6
    jihad is offline Member
    Join Date
    Oct 2011
    Posts
    24
    Rep Power
    0

    Default Re: for(d=1;d<1000;d++)for(c=1;c<d;c++)d%c!=0; what is wrong

    for(d=1;d<1000;d++)for(c=1;c<=d;c++){if(d%c==0){e+ +;if(e==2)System.out.println(d+"");}}
    why it isnt correct it should be correct but it gaves just 2 as an answer

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default Re: for(d=1;d<1000;d++)for(c=1;c<d;c++)d%c!=0; what is wrong

    Why do you think it should be correct? I can see quite a few funny things going on there that most certainly can never be correct: e.g. what is that test for e == 2 doing? For a next pass through the inner loop, e might never be equal to 2 anymore ... oh, and b.t.w. you don't really indent your code like that, do you?

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    jihad is offline Member
    Join Date
    Oct 2011
    Posts
    24
    Rep Power
    0

    Default Re: for(d=1;d<1000;d++)for(c=1;c<d;c++)d%c!=0; what is wrong

    if e has 2 value it means the number is primer

  9. #9
    jihad is offline Member
    Join Date
    Oct 2011
    Posts
    24
    Rep Power
    0

    Default Re: for(d=1;d<1000;d++)for(c=1;c<d;c++)d%c!=0; what is wrong

    yes you are right I didnt see :D it is really funny

Similar Threads

  1. Replies: 4
    Last Post: 06-11-2013, 01:37 AM
  2. Win 1000$ Now [Java WebApp]
    By biz in forum Java Software
    Replies: 1
    Last Post: 10-01-2011, 07:17 PM
  3. Perfect number 1-1000 program help
    By ImWithStupid in forum New To Java
    Replies: 9
    Last Post: 04-28-2011, 08:22 AM
  4. Guide to hacking a website with 1000 way
    By thanhquoc in forum Networking
    Replies: 1
    Last Post: 07-29-2010, 07:08 AM
  5. approx. 1000 instances of class?
    By artemff in forum CLDC and MIDP
    Replies: 0
    Last Post: 01-01-2010, 07:57 PM

Posting Permissions

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