Skip to content

Problem Solving with Nested Loop

Problem Set

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

??? example "Sin x" ## Sin x

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
Approximate $\text{sin}(x)$ using Taylor series with $k$ iterations.


### Task

Write Python code to compute and print the approximate of $\text{sin}$ of `#!py3 x` with `#!py3 k` number of iterations.


### Assumptions

- `#!py3 x` is a non-negative floating point between 0 and $2\pi$ (_i.e.,_ `#!py3 0 <= x < 6.28...`).
- `#!py3 n` is a non-negative integer (_i.e.,_ `#!py3 n >= 0`).
- `#!py3 x` and `#!py3 n` are initialized.


### Restrictions

- You cannot use the built-in `#!py3 sin` function from the `#!py3 math` module.

??? example "Cos x" ## Cos x

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
Approximate $\text{cos}(x)$ using Taylor series with $k$ iterations.


### Task

Write Python code to compute and print the approximate of $\text{cos}$ of `#!py3 x` with `#!py3 k` number of iterations.


### Assumptions

- `#!py3 x` is a non-negative floating point between 0 and $2\pi$ (_i.e.,_ `#!py3 0 <= x < 6.28...`).
- `#!py3 n` is a non-negative integer (_i.e.,_ `#!py3 n >= 0`).
- `#!py3 x` and `#!py3 n` are initialized.


### Restrictions

- You cannot use the built-in `#!py3 cos` function from the `#!py3 math` module.

??? example "Next Prime" ## Next Prime

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
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 `#!py3 n`.


### Assumptions

- `#!py3 n` is a non-negative integer (_i.e.,_ `#!py3 n >= 0`).
- `#!py3 x` is initialized.