# Write a c program to find nth prime number in a given range

But that's still not fast enough. It's evil! I am trying to figure out to even get a functioning code to do what I wanted it to do. I think you should change it to isPrime and then reverse the sense of it. I wasn't ignoring you. I was given partial code from my teacher that was written that way, which is the only reason I am even using isPrime I assume my teacher put it in there for a reason.

Your code as is returns 11, the 5th prime.

### C program to print prime numbers from 1 to n using for loop

It would be more clear to just say return instead of break. EDIT: Here is my updated code. Actually, you don't really need isPrime at all since you could just test your break condition again. Instead of iterating through all integers, you can only iterate through all odd integers. It isn't counting 2 as a prime number, which I think I understand why now. I don't think temp is a very good name. Now, I am going to try to see if I can clean it up a bit and make it run a bit faster. Wasting computation The way you are currently going about the problem is that you find each prime independently of the next. But for the purposes of your online problem, I'm sure that the key factor was to use a one pass solution instead of an N pass solution.

I don't think temp is a very good name. This reduces the problem by a factor of N, where N is the number of primes you were asked to find. This goes for both loops. It would be more clear to just say return instead of break.

A smart compiler will optimize this for you, but you can't always depend on your compiler being that smart. But for the purposes of your online problem, I'm sure that the key factor was to use a one pass solution instead of an N pass solution.

Instead of iterating through all integers, you can only iterate through all odd integers. It isn't counting 2 as a prime number, which I think I understand why now.

Rated 9/10
based on 81 review

Download