# Tutorial

We will here use a computer to gain some evidence to help tackle the following
problem.

```{admonition} Problem

Consider the following polynomial:

$$
    p(n) = n ^ 2 + n + 41
$$

1. Verify that $p(n)$ is prime for $n\in \mathbb{Z}$ up until $n=20$.
2. What is the smallest value of $n$ for which $p(n)$ is no longer prime?

```

We will start by defining a function for $p(n)$:


```python
def p(n):
    """
    Return the value of n ^ 2 + n + 41 for a given value of n.
    """
    return n ** 2 + n + 41
```

We will use `sympy` to check if a number is prime.


```python
import sympy as sym

sym.isprime(3)
```




    True




```python
sym.isprime(4)
```




    False



Now to answer the first question we will use a list comprehension to create a
list of boolean variables that confirm if $p(n)$ is prime.

```{tip}
This is similar to what we did in {ref}`probability`.
```


```python
checks = [sym.isprime(p(n)) for n in range(21)]
checks
```




    [True,
     True,
     True,
     True,
     True,
     True,
     True,
     True,
     True,
     True,
     True,
     True,
     True,
     True,
     True,
     True,
     True,
     True,
     True,
     True,
     True]



We can use the `all` tool to check if all the boolean values are true:


```python
all(checks)
```




    True



```{attention}
Using list comprehensions is a mathematical way of repeating code but at times
it might prove useful to repeat code in a different way using a standard `for`
statement.
```

In that case we can essentially repeat the previous exercise using:


```python
checks = []
for n in range(21):
    value = p(n)
    is_prime = sym.isprime(value)
    checks.append(is_prime)
all(checks)
```




    True



The main difference between the two approaches is that we can include multiple
lines of indented code to be repeated for every value of `n` in `range(21)`.

```{attention}
A `for` loop or a list comprehension should be used when we know how many
repetitions we want to make.
```

To answer the second question we will repeat the code until the value of $p(n)$
is no longer prime.


```python
n = 0
while sym.isprime(p(n)):
    n += 1
n
```




    40



```{attention}
A `while` loop should be used when we do not know how many times a repetition
should be made **but** we know under what conditions is should be made
```

Indeed for that value of $n$ we have:


```python
p(n)
```




    1681



and


```python
sym.isprime(p(n))
```




    False



`sympy` can also factor the number for us:


```python
sym.factorint(p(n))
```




    {41: 2}



Indeed:


```python
41 ** 2
```




    1681



c806c18ac30ac49772f3ed0e7482bc2e22a4f26b
ipynb
Jupyter Notebook
book/building-tools/01-variables-conditionals-loops/tutorial/.main.md.bcp.ipynb
11michalis11/pfm
c91b1eda70d7cde3fbe065db4667f84853947850
MIT
2020-09-24T21:02:41.000Z
2020-10-14T08:37:21.000Z
book/building-tools/01-variables-conditionals-loops/tutorial/.main.md.bcp.ipynb
11michalis11/pfm
c91b1eda70d7cde3fbe065db4667f84853947850
MIT
2020-09-21T15:54:23.000Z
2021-12-19T23:26:15.000Z
book/building-tools/01-variables-conditionals-loops/tutorial/.main.md.bcp.ipynb
11michalis11/pfm
c91b1eda70d7cde3fbe065db4667f84853947850
MIT
2020-10-02T09:21:27.000Z
2021-07-08T14:46:27.000Z
Qwen/Qwen-72B
1. YES
2. YES