Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JUST MATLAB CODE PLEASE!!!! Like the sieve of Eratosthenes, the sieve of Sundara

ID: 2085783 • Letter: J

Question

JUST MATLAB CODE PLEASE!!!!

Like the sieve of Eratosthenes, the sieve of Sundaram is a deterministic algorithm for finding a list of prime numbers. The algorithm can be described as follows. Starting from a list of integers from 1 to n, remove all the numbers of the form i + j + 2ij subject to the conditions: 1. i, j Element N 2. 1 lessthanorequalto i lessthanorequalto j 3. i + j + 2ij lessthanorequalto n Finally, double each number and increment each by 1. The resulting list is a list of primes below 2n + 2, except the number 2. Write a MATLAB function with the following signature: function [result] = sundaram(n) This function should return a vector of all primes below 2n + 2.

Explanation / Answer

Code:   

N = 100;
    n=1:(N/2)- 1;
    m=n;
    for p=m %For each number between 1 and n inclusive
        for i=1:p %'i' is all numbers greater than or equal to 1 up to p
            j=i:p; %'j' is all numbers greater than or equal to i up to p
            for k=i+j+2*i*j %Calculate the numbers to remove, and loop through them
                n(n==k)=[]; %Remove that value from the 'n' array
            end
        end
    end   
    result = [2;2*n'+1]

Output: