I'm making a program to output any prime number between 100 and 200 for my programming lesson. Now, I'm stuck. It seams everything would be working fine except for one error. In the end: { if (i/num = svar) System.out.println(+svar); else System.out.print(""); } it won't recognize the "/", saying that Task4.java [12:1] unexpected type required: variable found : value if (i/num = svar) ^ 1 error -Could anyone help me on this one?

In many computer language, a = b means you wnat to assign the value b to a; ex: a = 5 means you assign 5 to a; a == b means you want to compare a with b if they are equal; so, maybe you should modify " if (i / num == svar)".

Linnea Palin
Greenhorn

Joined: Oct 02, 2003
Posts: 12

posted

0

TY both, I prolly had been sitting for days wondering what was wrong otherwize... Anyways, I'm at home now, and I've changed somethings, and this is what I've got right now.. public class Task4 { public static void main (String[] args) { int i; for( i=200; i>=100; i--); { if (i%2==0) System.out.println(+i); else System.out.print(""); } } } now, the thingie is that in the line: if (i%2==0) i don't want ==. I wan't to state that if it does NOT equal 0, then ... Is there anyway to state the oposite to equals? As you all know, I'm fairly new to this, and I'm not english speaking so forgive me if i might mislead you sometimes If I didn't say earlier, the point of this is to output any prime number between 100 and 200. I got this hint from my teacher; "Any number which divides exactly in 8 is a factor if 8. The factors of 8 are 1, 2, 4 and 8. If a number(x) is a factor of another(y) then y % x = 0. A prime number has just two different factors, itself and 1."

if (i%2==0) i don't want ==. I wan't to state that if it does NOT equal 0, then ... Is there anyway to state the oposite to equals? To test the inequality of primitive types and the references, you would use the "!=" operator. To test the logical inequality of objects, you would override the equals(Object o) method and use the !(object.equals(otherObject)) construct. So, in your case, what you want is

Now, since your task is to find primes, I am not sure why you are dividing by 2 only. If number N is a prime candidate, shouldn't you try to divide it by all numbers in the range of [2.. N/2]? [ October 02, 2003: Message edited by: Eugene Kononov ]

"If number N is a prime candidate, shouldn't you try to divide it by all numbers in the range of [2.. N/2]?" actually, to save time, you only have to go from 2 to the square root of N.

fred

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

Linnea Palin
Greenhorn

Joined: Oct 02, 2003
Posts: 12

posted

0

Ok, this is the latest version: public class TestTask4 { public static void main (String[] args) { int i; int x; for(i=100; i<200; i++); for(x=2; x<i; x++); { if (i%x != 0) System.out.println(+i+" is a prime number"); if (i%x == 0) System.out.println(+i+" is not a prime number"); else System.out.println(+i+" is not a prime number"); } } }

but the loop doesn't work. what's wrong. something must be wrong cause when it has gotten one output, then it will stop and not loop again...

John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937

posted

0

Fred: actually, to save time, you only have to go from 2 to the square root of N. In fact, I think it will suffice to only go through all the primes between 2 and square root of N. Linnea: for(i=100; i<200; i++); but the loop doesn't work. Your loop doesn't have a body, as indicated by the semicolon at the end of the "for" construct, -- that's why it doesn't do anything (except incrementing the counter). Next time you post code, please use the *code* UBB, -- it will preserve formatting and will make it easier for us to read your code. [ October 02, 2003: Message edited by: Eugene Kononov ]

Eugene:In fact, I think it will suffice to only go through all the primes between 2 and square root of N. yes, this is true. However, that requires a major re-working of the algorithm, which was why i didn't suggest it. changing from N/2 to SQRT(N) should give a significant performance boost (probably not noticable in this small data range, but still...), and would be trivial to implement. But thanks for keeping me honest!!! f

Linnea: The reason your loops won't work is that you are following the for statements immediately with semicolons. This causes the loops to repeat an empty statement. (Empty statements are legal in Java and, in this case, won't produce a compiler error.) Specifically, I am talking about these two for loops.

After you remove the trailing semicolons, your code still will not work properly. You have a couple of logic errors. Because this is a homework assignment, it isn't proper for me to fix them for you (since its the point of the assignment), but here is hint: To determine if a number is prime, you must try dividing it by all its possible factors, as you are trying to do. If **none** of them divide evenly, then the number is prime. If one does divide evenly, the number is not prime and you can stop looking.

For my latest books on Java, including Introducing JavaFX 8 Programming, see HerbSchildt.com

Linnea Palin
Greenhorn

Joined: Oct 02, 2003
Posts: 12

posted

0

Most recent code:

I think that part about the square root if i makes sence, but still, I've tried some different ways i thought could state SQRT(i), but none works.. So, how do you state the squareroot of something?

Hint #1: you don't have to take a squre root, you can calculate a square. Hint #2: If you really want to calculate a square root anyway (which may lead to faster code in the long run), look through classes in the java.lang package to see if you can find a class or method that looks like it might help. If you haven't learned about classes and packages, nevermind, and stick to the first hint. [ October 04, 2003: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister

Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671

posted

0

Actually though, I think the square root thing is sort of a distraction right now; your code has other more serious problems. I'd say, just focus on getting your code to print out some correct resultsfirst; then worry about making it faster/better. For now, using x<i is fine I think.

John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937

posted

0

Jim: Hint #1: you don't have to take a squre root, you can calculate a square. I didn't sleep all night thinking of what the heck you meant here, Jim. I think this would make it a good topic in the Programming Diversions forum.

You can use Math class to find the square root. Use pow(double a, double b)

why not just use Math.sqrt(double a)??? also, whoever said "get it to work, then optimize" is correct. here's another way to think about this. it's much easier to prove something is not prime, that prove it IS prime (at least for small numbers). in other words, as soon as you find a single divisor of your number-in-question (i mod j == 0), you can quit testing it, as you know it's NOT prime.