4 Problems on Introduction to Computing Using MATLAB | CS 1112, Assignments of Computer Science

Material Type: Assignment; Class: Introduction to Computing Using MATLAB; Subject: Computer Science; University: Cornell University; Term: Spring 2009;

Typology: Assignments

Pre 2010

Uploaded on 08/30/2009

koofers-user-4p5-1
koofers-user-4p5-1 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS1112 Section Exercise 5
1. Write a function aPrime(m) that 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.
3. [Modified from old prelim] The value of π/8 can be approximated by the series
1+ 1
32+1
52+1
72+···
Let the sum of the first nterms be Tn.Asnincreases one expects the ratio Tn
Tn+1 to approach 1. Write
a script to find the smallest nsuch that the ratio Tn
Tn+1 >.9999. Display nand Tn.Do not use arrays.
Hint: You need to keep track of a current sum and the next sum in the loop.
1
pf2

Partial preview of the text

Download 4 Problems on Introduction to Computing Using MATLAB | CS 1112 and more Assignments Computer Science in PDF only on Docsity!

CS1112 Section Exercise 5

  1. Write a function aPrime(m) that 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.
  3. [Modified from old prelim] The value of π/8 can be approximated by the series

Let the sum of the first n terms be Tn. As n increases one expects the ratio (^) TTnn+1 to approach 1. Write

a script to find the smallest n such that the ratio (^) TTn+1n > .9999. Display n and Tn. Do not use arrays.

Hint: You need to keep track of a current sum and the next sum in the loop.

  1. For each of the following sub-problems, complete the program so that it produces the desired result. You should not modify the given code in any way—only fill in the blanks that are provided. In every case you will need to use for-loops with an “increment” that is not one.

(a) The following program reads in a real number x and an integer N , and computes the sum ∑N k=

(−1)kx^2 k (2k)! to the first^ N^ terms. (This sum converges to cos(x) as^ N^ → ∞.) x = input(’Please input a real number between 0 and pi/2: ’); N = input(’Please input a positive integer: ’); total = 0;

for j = _______________________ total = total + (-1)^(j/2) * x^j / factorial(j); end fprintf(’The sum of the first %d terms is %12.8f\n’, N, total);

(b) The following does the same thing as in part (b), but this time we are not allowed to use expo- nentiation and the factorial function, and must compute these explicitly.

x = input(’Please input a real number between 0 and pi/2: ’); N = input(’Please input a positive integer: ’); total = 1; % Explicitly assign the first term (when j=0) sign = 1; % The sign of a term, either 1 or - jfact = 1; % Current value of j! xtoj = 1; % Current value of x^j

for j = _________________________

sign = ________________________________;

jfact = ________________________________;

xtoj = ________________________________;

total = ____________________________________________; end fprintf(’The sum of the first %d terms is %12.8f\n’, N, total);