Generate Prime Numbers
Write a function to generate all prime numbers less than or equal to a given integer n.
A prime number is a natural number greater than 1 that is not a product of two smaller natural numbers. For example, 2, 3, 5, and 7 are prime numbers, but 4, 6, 8, and 9 are not.
Your task is to implement the function generatePrimes
Input
An integer n where 1 <= n <= 10^4.
Output
A list of integers containing all the prime numbers less than or equal to n.
Example 1:
Input: n = 10
Output: [2, 3, 5, 7]
Example 2:
Input: n = 1
Output: []
Example 3:
Input: n = 19
Output: [2, 3, 5, 7, 11, 13, 17, 19]
Constraints
The function should be efficient and run in a reasonable time frame for the given constraints. You may assume the input is a positive integer.
Hint
Use the Sieve of Eratosthenes to optimize the solution by eliminating non-prime numbers systematically.
Loading...
Output will appear here…