lab2 ex4
You cannot submit for this problem because the homework's deadline is due.
Sieve of Eratosthenes is an algorithm used to find all prime numbers up to a given limit. Here is how it works:
Given an upper limit \(n\), Create an array containing all integers from 2 to \(n\).
Find the first element, which is 2.
Delete all multiples of 2. In other words, remove 4, 6, 8... from the array.
Find the first element in the array excluding 2, which is 3, and delete all multiples of 3. In other words, remove 9, 15, 21... from the array. (6, 12, 18... have already been removed!)
Find the first element in the array excluding 2 and 3, which is 5. Delete all multiples of 5.
Repeat this process until the first element in the array excluding those prime numbers that have already been processed is larger than \(n/2\).
Now left in the array are all the prime numbers in \([2,n]\).
Implement this algorithm! Take an integer \(n>2\) from user input and print a vector containing all prime numbers in \([2,n]\) in increasing order.
Sample Test Case
Input:
29
Output:
2 3 5 7 11 13 17 19 23 29
Format
You should submit a compressed file containing a Matlab source file ex4.m