# Help - boolean

Linnea Palin

Greenhorn

Posts: 12

posted 12 years ago

- 0

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 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?

Chao Chihwai

Greenhorn

Posts: 5

Linnea Palin

Greenhorn

Posts: 12

posted 12 years ago

- 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."

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."

John Smith

Ranch Hand

Posts: 2937

posted 12 years ago

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 ]

- 0

**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?

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 ]

posted 12 years ago

- 0

"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

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

Posts: 12

posted 12 years ago

- 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...

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

Posts: 2937

posted 12 years ago

In fact, I think it will suffice to only go through all the

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 ]

- 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.

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 ]

posted 12 years ago

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
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

- 0

**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

Marilyn de Queiroz

Sheriff

Posts: 9059

12

posted 12 years ago

- 0

Originally posted by Linnea Palin:

Linnea,

Notice how much nicer the code looks, how much easier to read, when it's in

[ October 03, 2003: Message edited by: Marilyn de Queiroz ]

Linnea,

Notice how much nicer the code looks, how much easier to read, when it's in

**code**tags.[ October 03, 2003: Message edited by: Marilyn de Queiroz ]

JavaBeginnersFaq

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt

Herb Schildt

Author

Ranch Hand

Ranch Hand

Posts: 253

6

posted 12 years ago

- 0

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.

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

Posts: 12

Jim Yingst

Wanderer

Sheriff

Sheriff

Posts: 18671

posted 12 years ago

- 0

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 ]

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

Sheriff

Posts: 18671

posted 12 years ago

- 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.

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

John Smith

Ranch Hand

Posts: 2937

Sainudheen Mydeen

Ranch Hand

Posts: 218

posted 12 years ago

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.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

- 0

Originally posted by Sainudheen Mydeen:

You can use Math class to find the square root. Usepow(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.