Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

MATLAB Consider the pseudocode for computing an array of prime numbers less than or equal to n. INPUT n - positive integer OUTPUT p is

MATLAB image text in transcribed
image text in transcribed
Consider the pseudocode for computing an array of prime numbers less than or equal to n. INPUT n - 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.) i=0 for c = 2,...n for d = 2, 3,...k-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 pi=c 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... 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 If n 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 n The first line of your code should therefore read: function p - primelist(n). Consider the following alternate method for computing a list or prime numbers. (known as the Sieve of Eratosthenes) INPUT n - 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.) p= array of integers 1 through n P = 0. for k = 2,3,..., If Pk 70 then set Pj = 0 for j = k?, ka + k, k? + 2k, ..., end If statement end for loop Extract all nonzero elements of the array p. 4. (20 points) Create a Matlab function, titled prime sieve that implements the pseu- docode above. Your function should have the following input and output: Input 11 - positive integer Output p- an array of prime number less than or equal to n The first line of your code should therefore read: function p - prime sieve(n). 5. (20 points) Write a script titled HW4.m that uses the tic and toc functions to plot in the same figure the amount of time each of the three codes requires to produce the prime number list as a function of n for values of n=10', 10, ..., 10%. Use a loglog scaling in your plot. Bonus (20 points) Create a Matlab function titled prime.count.m that takes as an input n, and returns the array N", where * = (k) is the number of prime numbers less than or equal to k (for example, 02 = (2) = 1, 0 = 7(5) = 3, no = (10) = 4) for k = 1,2,...n. Use that function to create a plot of (k) vs. k for k = 2, 3, 4, ..., 10. In order to receive credit, your code must call a prime generating function exactly once! to compute (k) for k=1,2,..., n

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Question

What keeps gage blocks together when they are "wrung together"?

Answered: 1 week ago