Skip to content

Problem Solving with Nested Loop

Problem Set

The following problem sets are intended for practice. Attempt these on your own.

Sin x

Sin x

Approximate \(\text{sin}(x)\) using Taylor series with \(k\) iterations.

Task

Write Python code to compute and print the approximate of \(\text{sin}\) of x with k number of iterations.

Assumptions

  • x is a non-negative floating point between 0 and \(2\pi\) (i.e., 0 <= x < 6.28...).
  • n is a non-negative integer (i.e., n >= 0).
  • x and n are initialized.

Restrictions

  • You cannot use the built-in sin function from the math module.
Cos x

Cos x

Approximate \(\text{cos}(x)\) using Taylor series with \(k\) iterations.

Task

Write Python code to compute and print the approximate of \(\text{cos}\) of x with k number of iterations.

Assumptions

  • x is a non-negative floating point between 0 and \(2\pi\) (i.e., 0 <= x < 6.28...).
  • n is a non-negative integer (i.e., n >= 0).
  • x and n are initialized.

Restrictions

  • You cannot use the built-in cos function from the math module.
Next Prime

Next Prime

Given an integer \(n\), we want to find the number \(m\) such that \(m \geq n\) and \(m\) is a prime number.

Task

Write Python code to compute and print the first prime number that is larger than or equal to the input n.

Assumptions

  • n is a non-negative integer (i.e., n >= 0).
  • x is initialized.