MATLAB Finding Prime Numbers Up to N: A Scientific Approach

AI Thread Summary
To write a program that finds all prime numbers less than or equal to a user-supplied integer N, the approach involves checking divisibility only by prime numbers up to the square root of N. For example, with N=60, the process starts by identifying all integers from 2 to 60 and checking each for primality. A function can be implemented to determine if a number is prime by testing divisibility against known primes less than or equal to the square root of that number. This reduces unnecessary calculations and optimizes the process. Additionally, a function to check if a division results in an integer can be useful for confirming non-primality. This method effectively streamlines the identification of prime numbers through efficient checks and calculations.
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.
 
Back
Top