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

Desperate for help with Matlab Code!! can\'t figure this problem out! An upper l

ID: 3790334 • Letter: D

Question

Desperate for help with Matlab Code!! can't figure this problem out!

An upper limit, n Outputs: A row vector of prime numbers primes isPrime ismember setdiff(), factor Conditionals (if and switch statements), iteration (for and while loops) You are working at the great Library of Alexandria. It is the year 190 B.C. Your best buddy and chief librarian, Eratosthenes of Cyrene, comes to you with a problem. He thinks he has come up with a way to list all of the prime numbers less than or equal to a given number n He is not sure how well it will work, though, so he asks you to implement it in MATLAB ancient Greek style. He tells you to check out this article for some very cool instructions and visualizations The idea is to start with all of the numbers from 1 to n, and eliminate multiples of 2, then multiples of 3, then multiples of 4, etc. until only prime numbers remain. However, Eratosthenes noticed that you only have to check for divisibility by numbers up to floor (sqrt (n)) Your function should behave exactly the same way as the built in function primes(), but of course this function is banned! If you know about conditionals and/or iteration from past coding experience, it may be tempting to try to use it for this problem. However, there is a way to solve the problem using only simple math, arrays, and indexing The input will always be a positive integer 1 is not prime! If the input number is prime, include it in your output. Use of iteration or conditionals to solve this problem will receive no credit

Explanation / Answer

function primalitytest = primality(n)
n = input('Enter value of n: ')

for i = 1:n
m = 2; % initialise factor to test
for m = 2:floor(sqrt(n))
if mod(n,m) == 0 %m is a factor of n
disp('n is not prime')

else

disp(n);
end
end;

Here the first for loop is to iterate upto n and second for loop is for primality testing.

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