Results 1 to 9 of 9
- 10-30-2011, 12:51 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 24
- Rep Power
- 0
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.
-
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.
- 10-30-2011, 01:13 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 10-30-2011, 01:46 PM #4
Member
- Join Date
- Oct 2011
- Posts
- 92
- Rep Power
- 0
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 :)
- 10-30-2011, 02:06 PM #5
Member
- Join Date
- Oct 2011
- Posts
- 24
- Rep Power
- 0
Re: for(d=1;d<1000;d++)for(c=1;c<d;c++)d%c!=0; what is wrong
thanks alot
- 10-30-2011, 02:46 PM #6
Member
- Join Date
- Oct 2011
- Posts
- 24
- Rep Power
- 0
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
- 10-30-2011, 02:55 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
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?
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 10-30-2011, 03:24 PM #8
Member
- Join Date
- Oct 2011
- Posts
- 24
- Rep Power
- 0
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
- 10-30-2011, 03:27 PM #9
Member
- Join Date
- Oct 2011
- Posts
- 24
- Rep Power
- 0
Similar Threads
-
Win 1000$ Now [Java WebApp]
By biz in forum Java SoftwareReplies: 1Last Post: 10-01-2011, 07:17 PM -
Perfect number 1-1000 program help
By ImWithStupid in forum New To JavaReplies: 9Last Post: 04-28-2011, 08:22 AM -
Guide to hacking a website with 1000 way
By thanhquoc in forum NetworkingReplies: 1Last Post: 07-29-2010, 07:08 AM -
approx. 1000 instances of class?
By artemff in forum CLDC and MIDPReplies: 0Last Post: 01-01-2010, 07:57 PM -
Wrong output (well.. the one who's wrong is probably me ;) )
By shacht1 in forum New To JavaReplies: 2Last Post: 11-22-2009, 03:48 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks