i have sample for to find out prime number , but i want to optimise is more. can anyone plz help me.

int i;
for (i=2; i < num ;i++ ){
  int n = num%i;
  if (n==0){
    System.out.println("not Prime!");
if(i == num){
  System.out.println("Prime number!");

thnaks in adavance.

Edited by mike_2000_17: Fixed formatting

Votes + Comments
Use Code TAGS
7 Years
Discussion Span
Last Post by Adak

This firstly seems like JAVA code to me.
However there are several optimizations that you can do in order to decrease the number of iterations that are taking place.

One of them would be to decrease

i < num


i < num/2

There are other algorithms too.. to find Prime numbers try implementing them.

Votes + Comments
use [ICODE] instead of [CODE]

It's hard to take your request seriously when you post up some code that won't even run.

There are lots of other posts on this subject, here and all over the net. Also, check out the Sieve of Eratosthenes, on Wikipedia, etc.

Come back with a full program, and then we'll talk turkey on your speed up. ;)


I would like to comment upon the reputation point deduction made by thekashyap.

The way I would like to show code is rather my own will. Its not like I didnt know the existence of ICODE. I felt it would look rather more appealing to the OP when the code is highlighted in the code tag, rather than the change in font text and a highlight.


VS has posted 13 times - past due for the OP to start using code tags.

Deducting rep points for using code tags, instead of the (more concise) icode tags, is a bit too much control, imo.

Votes + Comments
Agreed. If you like CODE rather than ICODE, go for it...
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.