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?
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)".
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."
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 ]
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 offbyone errors
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...
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 ]
yes, this is true. However, that requires a major reworking 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 offbyone errors
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
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
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
Sheriff
"I'm not back."  Bill Harding, Twister
Originally posted by Sainudheen Mydeen:
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 numberinquestion (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 offbyone errors
All of life is a contant education  Eleanor Roosevelt. Tiny ad:
The WEB SERVICES and JAXRS Course
https://coderanch.com/t/690789/WEBSERVICESJAXRS
