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!");
    break;
  }
}
if(i == num){
  System.out.println("Prime number!");
}

thnaks in adavance.

Sky Diploma commented: Use Code TAGS -1

Recommended Answers

All 4 Replies

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

to

i < num/2

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

commented: use [ICODE] instead of [CODE] -1

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.

commented: Agreed. If you like CODE rather than ICODE, go for it... +15
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.