lab2 ex4

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:

  1. Given an upper limit \(n\), Create an array containing all integers from 2 to \(n\).

  2. Find the first element, which is 2.

  3. Delete all multiples of 2. In other words, remove 4, 6, 8... from the array.

  4. 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!)

  5. Find the first element in the array excluding 2 and 3, which is 5. Delete all multiples of 5.

  6. Repeat this process until the first element in the array excluding those prime numbers that have already been processed is larger than \(n/2\).

  7. 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

Lab2

Not Claimed
Status
Finished
Problems
6
Open Since
2023-05-23 18:00
DDL
2023-05-25 22:00
Extension
0.0 hour(s)