Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

using MatLab, please help! INPUT n - positive integer OUTPUT p is an array of prime numbers less than or equal to n. (Tn) is

using MatLab, please help! image text in transcribed
image text in transcribed
INPUT n - positive integer OUTPUT p is an array of prime numbers less than or equal to n. (Tn) 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.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 If n is not divisible by any integer between two and n, then n is prime. 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 = prime list(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 Pi = 0. for k = 2,3,.... n If Pk 70 then set p; = 0 for j = K2, 42 + k, k2 + 2k, ...,n 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 n - positive integer Output p- an array of prime number less than or equal to 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_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

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

Get Started

Recommended Textbook for

DB2 Universal Database V7.1 Application Development Certification Guide

Authors: Steve Sanyal, David Martineau, Kevin Gashyna, Michael Kyprianou

1st Edition

0130913677, 978-0130913678

More Books

Students also viewed these Databases questions

Question

Explain the process of hearing.

Answered: 1 week ago