CS100M Section Exercise 5: Writing Functions for Primes and Twin Primes - Prof. K. Fan, Assignments of Computer Science

Instructions for writing two functions in matlab: aprime(m) to check if a number is prime, and lasttwinpair(n) to find the last twin prime pair smaller than or equal to a given number n. The document also includes a brief explanation of vectors and how to access their components.

Typology: Assignments

Pre 2010

Uploaded on 08/30/2009

koofers-user-ixk
koofers-user-ixk 🇺🇸

5

(1)

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS100M Section Exercise 5
1. Write a function aprime(m) that has an input parameter m. Function aprime(m) returns 1 if
mis prime, and 0 otherwise. Remember to write a concise comment to describe the function, including
its parameters under the function header.
2. Atwin prime is a pair of primes such that if pis a prime, p+ 2 is also a prime. The larger prime
in the pair is called the big prime, while the smaller prime is called the little prime. For example, in the
twin prime pair (3,5), 5 is the big prime while 3 is the little prime. Write a function lastTwinPair(n)
that will, given a number ngreater than or equal to 5, return the last (largest) twin prime pair smaller
than or equal to n.Use function aprime from the previous question! This function returns two values.
Call them littlep and bigp.
1
pf2

Partial preview of the text

Download CS100M Section Exercise 5: Writing Functions for Primes and Twin Primes - Prof. K. Fan and more Assignments Computer Science in PDF only on Docsity!

CS100M Section Exercise 5

  1. Write a function aprime(m) that has an input parameter m. Function aprime(m) returns 1 if m is prime, and 0 otherwise. Remember to write a concise comment to describe the function, including its parameters under the function header.
  2. A twin prime is a pair of primes such that if p is a prime, p + 2 is also a prime. The larger prime in the pair is called the big prime, while the smaller prime is called the little prime. For example, in the twin prime pair (3,5), 5 is the big prime while 3 is the little prime. Write a function lastTwinPair(n) that will, given a number n greater than or equal to 5, return the last (largest) twin prime pair smaller than or equal to n. Use function aprime from the previous question! This function returns two values. Call them littlep and bigp.
  1. A vector is a one-dimensional list of values. Let variable v be a vector; its length can be obtained using a built-in function: length(v). To access just one component (value) in vector v, specify the position of that component: v(k) where k = 1, 2 ,... , length(v). For example, the following fragment will print all the values in a given vector v:

%Given vector v, print its values for k= 1:length(v) fprintf(’%d ’, v(k)) end fprintf(’\n’)

Complete the following function. The only built-in function that you can use is length.

function [val, ind] = myMin(v) % val is the minimum value in vector v. % ind is the position of the first occurence of val in v.