sec5 - CS100M Section Exercise 5 1 Write a function...

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

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
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

  • Summer '07
  • FAN/VANLOAN
  • Number Theory, Binary relation, Prime number, twin prime pair

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern