Finding primes up to N by dividing only up to sqrt(N)

Join the discussion
Registration is free. Start your own thread to ask a follow-up.
2 replies · 3K views
TimNguyen
Messages
79
Reaction score
0
How would I write a program that finds all the prime numbers that are less than or equal to a "user-supplied" integer N, implementing the fact that I should only be dividing N by all prime numbers less than sqrt(N)?
 
Physics news on Phys.org
Don't sound like a [strictly-] MATLAB question... but more like a homework problem.

How would you do it by hand with (say) N=60?
 
there should be a function that checks whether a resulting calculation is an integer or not (or you can always make your own function).

That's the key to this.