|
Re: I HAVE DONE THE WORK - NEED GUIDANCE !!!
|
Posted: Apr 12, 2004 12:26 AM
|
|
You missed something trivial indeed. Your brackets don't include the right stuff. The closing bracket right before sqrtnum=(int)Math.sqrt(num) line should not be there. You probably missed that because you have a tab on the line num=Integer.parseInt(stdin.readline()), which does not belong there. So delete that ending-bracket, and add it at the end of the while-loop, just before the last two ending-brackets. Furthermore, you can optimalize the while loop by rewriting it to while (i<sqrtnum && num%i!=0) i++ The while-loop then ends as soon as it has found a divisor, your algorithm goes on after that until sqrtnum has been reached. Also, first try checking divisibility by 2, and then increment i from 3 with steps of 2 (i+=2 instead of i++). This excludes the unnescessary checks of 4,6,8 etc if 2 was not a divisor (if num%2!=0, num%4!=0). Hope this helped you.
|
|