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

Write a script using MATLAB that prompts the user for a number and then finds al

ID: 3683425 • Letter: W

Question

Write a script using MATLAB that prompts the user for a number and then finds all of the primes up to that number by using the sieve of Eratosthenes.

The sieve of Eratosthenes is a simple, ancient algorithm for finding all prime numbers up to any given limit. It does so by iteratively marking as composite (i.e., not prime) the multiples of each prime, starting with the multiples of 2.

To find all the prime numbers less than or equal to 20, proceed as follows. First generate a list of integers from 2 to 20: 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

The first number in the list is 2; cross out every 2nd number in the list after it by counting up from 2 in increments of 2 (these will be all the multiples of 2 in the list): 2 3 5 7 9 11 13 15 17 19

The next unmarked number in the list after 2 is 3; cross out every 3rd number in the list after it by counting up from 3 in increments of 3 (these will be all the multiples of 3 in the list): 2 3 5 7 11 13 17 19

The next unmarked number in the list after 3 is 5; the next step would be to cross out every 5th number in the list after 5, but they are all already crossed out at this point, as these numbers (10, 15, 20) are also multiples of smaller primes because 5*5 is greater than 25. The numbers left not crossed out in the list at this point are all the prime numbers below 20: 2 3 5 7 11 13 17 19

Please post a screenshot of the whole answer and a copy of the code too.

Example:

n? 20

2 3 5 7 11 13 17 19

Explanation / Answer

n=input('Enter value of n : ');
prime_numbers=[];
for number=2:n
result=true;
for k=2:(number/2)
if rem(number,k)==0
result=false;
break;
end
end
if(result==true)
prime_numbers=[prime_numbers number];
end
end
disp(prime_numbers);

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote