Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

MATLAB INPUT -positive integer OUTPUT p is an array of prime numbers less than or equal to n. (*(n) is the number of prime numbers

image text in transcribed
image text in transcribed
image text in transcribed
MATLAB
INPUT -positive integer OUTPUT p is an array of prime numbers less than or equal to n. (*(n) is the number of prime numbers less than or equal to n.) for 2...n for d = 2, 3, ...e-1 if remainder(c/d) = 0 then c is composite, stop loop and go to next c value end if statement end for loop If inner loop cycled all the way through then i =i+1 then pc end if statement end for loop The algorithm can be summarized in this way. Consider whether each integer from 2 to n is prime by dividing it by all integers greater than 1 and less than the number being considered. If one of those lesser numbers divides evenly into our candidate prime, then it's composite. Otherwise it is prime and we add that candidate number to the list. This algorithm is implemented in the code prime.list slow.m. As the code name sug- gests, we can accelerate the speed of the code. That is the subject of this homework. Theorem 1 If n is not divisible by any prime number less than n, then n is a prime number Theorem 2 In is not divisible by any integer between two and then n is prime. 1. (20 points) Explain how the the two theorems above can be used to accelerate the code prime list slow.m created in class (and available via Blackboard). 2. (20 points) Write a pseudocode incorporating these changes with input n and output PEN), which is an array of all prime numbers less than or equal to n. 3. (20 points) Create a Matlab function titled prime list that implements your pseu- docode above. Your function should have the following input and output: Input n - positive integer Output p- an array of prime number less than or equal to The first line of your code shonk therefore real function p prime liat (n) Consider the following alternate method for computing a list of prime numbers (known is the Sieve of Eratosthenes LILEILL If one of thos numbers divides evenly into our candidate prime, then it's composite. Otherwise it is prime and we add that candidate mumber to the list. This algorithm is implemented in the code prime list slow... As the code name sug gests, we can accelernte the speed of the code. That is the subject of this homework. The 1. Theorem 1 Ifn is not divisible by any prime number less than n, then n is a prime number Theorem 2 is not divisible by any integer between two and Vn, then ni prime 1. (20 points) Explain how the the two theorems above can be used to accelerate the code prime listublov.m created in class and available via Blackboard). 2. (20 points) Write perdocode incorporating these changes with input n and output PENT which is an array of all prime numbers less than or equal to n. (20 points) Create a Matlab function, titled prime list that implements your pseu- docode above. Your function should have the following input and output: Input n - positive integer Output p- an array of prime number less than or equal ton The first line of your code should therefore read: function p prime list (n) Consider the following alternate method for computing a list or prime numbers (known as the Sieve of Eratosthenes) ) in the number INPUT n - positive integer OUTPUT PINATAY of prime mimbers less than or equal ton of prime numbe less than or equal to ne Pe array of inte ens through for = 2.3... 26 then st o r . end INtatement end for loop Extract all nonzero elements of the TAN ? Editor - Usersist vera ab Download printeist slow.m" e function p - primeletalow (n) ALLOCATE SPACE FOR PRIME LIST 1 MEANS ROUND TOWARDINE zeros (ceil(n/2), 1); INITIALIZE pcount to o LOOP OR CYCLE THROUGH EVERY NUMBER THROUGH N TO TEST FOR PRIMENESS 2n ASET/RESET comptlag comp tlag - 0 LOOP THROUGH POSSIBLE DIVISORS Ford-2 -1 mod (c,d) - 0) T/E QUESTION ABOVE IS TE LOOMBOSILE SET COMPOSITE FEING TO compeflag 1; break COLE THOUGH ALL POSSIBLE DIVISORS THE SECOND FOR LOOP) AND DIDRE END 1. DIVISOR, THEN 1. emellago P MARE L E COSTER pe : me sound CREATEELES

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions