sec5 - 1 1 3 2 1 5 2 1 7 2 Write a script that...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
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 3, 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. [From Prelim 1] The value of π/ 8 can be approximated by the series
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 + 1 3 2 + 1 5 2 + 1 7 2 + · · · Write a script that evaluates this expression, ignoring all terms that are strictly smaller than .000001. Your script should display the number of terms summed and the sum. Use a while-loop. Think about the initialization of your variables and the order of computation carefully! In the loop body you need to calculate the value of a term only once. 4. We use the above series for approximating π/ 8 again but with a different stopping criterion. Let the sum of the ±rst n terms be T n . As n increases one expects the ratio T n T n +1 to approach 1. Write a script to ±nd the smallest n such that the ratio T n T n +1 > . 9999. Display n and T n . Hint: Now you need to keep track of a current sum and the next sum in the loop. 1...
View Full Document

This lab report was uploaded on 02/15/2008 for the course CS 100 taught by Professor Fan/vanloan during the Fall '07 term at Cornell.

Ask a homework question - tutors are online