Given an integer M. return all prime numbers smaller than M.
Give a algorithm as good as you can. Need to consider time and space complexity.
List of prime numbers less than one million. In fact, there are 78,498 prime numbers less than 1,000,000=one million.
A prime number is a whole number greater than 1 whose only factors are 1 and itself. A factor is a whole number that can be divided evenly into another number. The first few prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23 and 29. Numbers that have more than two factors are called composite numbers.
The prime numbers from 1 to 100 are: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97. Why is 1 not a prime number?
The Sieve of Eratosthenes is a good place to start.
http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
A couple of additional performance hints:
M
, since every composite number has at least one prime factor less than or equal to its square rootsqrt(M)
)2
, of course)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With