![generate list of prime numbers generate list of prime numbers](https://static.listoffreeware.com/wp-content/uploads/cosmos_13th_prime_numbers_generator_and_tester_free_prime_factorization_calculator_2017-07-06_12-02-19.png)
This list is much shorter than the list of all odd numbers not divisible by 3, as the IsPrime function above does. For example, why would we divide one number with all smaller numbers? Since we are finding all prime numbers, we already know the list of all prime numbers smaller than current number. What we could do next is to try to optimize this algorithm. The IsPrime function is copied from Testing if Number is Prime The function which operates on this principle might look like this: function ExtractPrimesBruteForce(n) To cut the long story short, here is the solution: Second function would again be the square root curve, but the one passing over each element of the sum. First function would be the square root curve which passes under each element of the sum. Basically, exact value of the sum lies between values of integrals of two functions. But we can still calculate its asymptotic bounds with some help of integrals. Unfortunately, this sum doesn’t have a closed form. This means that total number of steps required to extract all prime numbers not exceeding N is:
![generate list of prime numbers generate list of prime numbers](https://www.math-drills.com/numbersense/images/prime_factors_1000_to_9999_list_pin2.jpg)
Then testing each number k takes square root of k to complete. Thus it seems feasible to simply iterate through all the numbers and test each of them. If no divisors are found, then the number is officially declared prime.
![generate list of prime numbers generate list of prime numbers](https://i.ytimg.com/vi/Cg329am1SOg/maxresdefault.jpg)
, we have argued that it is possible to test whether one number is prime by trying all numbers greater than one which do not exceed its square root as possible divisors. In the following sections we are going to try several approaches to solving the problem. Given a value N, N > 2, write a function which returns an array with all prime numbers that are not greater than N.Įxamples: For N=20, the function should return array.