---
title: "Practical 03"
author: 'Student ID: 2057444;  Lab Group: 27'
date: "3/10/2022"
output: pdf_document
---

```{r setup, include=FALSE}
# Set here the general options
knitr::opts_chunk$set(echo = TRUE, eval = TRUE)
```

## 1. Vectors

Give one command/line of code which will generate the following:

(i) (-8, -5, -2, 1, 4, 7, 10, 13)

```{r Q1-i}
# This is an R chunk, you can treat is as similar to a script
# Here you could insert your answers to the exercises
c(-8, -5, -2, 1, 4, 7, 10, 13) 
```

(ii) (2, 2, 2, 4, 4, 4, 6, 6, 6, 8, 8, 8, 10, 10, 10, 12, 12, 12)

```{r Q1-ii}
# This R chunk is named Q1-ii and it inherits the general option echo = TRUE, eval = TRUE 
c(2, 2, 2, 4, 4, 4, 6, 6, 6, 8, 8, 8, 10, 10, 10, 12, 12, 12)
```

(iii) (9, 9, 9, 9, 9, 9, 6, 6, 6, 6, 6, 3, 3, 3, 3, 0, 0, 0, -3, -3, -6)

```{r Q1-iii, echo = TRUE, eval = TRUE}
# This R chunk is named Q1iii and it is explicitly told to have parameters echo = TRUE, eval = TRUE 
c(9, 9, 9, 9, 9, 9, 6, 6, 6, 6, 6, 3, 3, 3, 3, 0, 0, 0, -3, -3, -6)
```

(iv) (1, 8, 81, 1024, ..., 3486784401, 100000000000) (Note: 1 = 12,8 = 23,81 = 34,...,100000000000 = 1011.)

```{r Q1-iv, echo = TRUE, eval = FALSE}
# If eval = FALSE then the code is not evaluated, but this chunk is still echoed
c(1, 8, 81, 1024, 3486784401, 100000000000) 
```

(v) (-3, 0, 3, -9, 0, 9, -27, 0, 27, -81, 0, 81, -243, 0, 243)

```{r Q1-v}
# What happens if you set echo=FALSE? 
c(-3, 0, 3, -9, 0, 9, -27, 0, 27, -81, 0, 81, -243, 0, 243)
```

\pagebreak

## 2. Random Numbers and vectors

1.  Create a vector `x` which will contain 1000 realisations from a Normal(8, 3) distribution.

```{r Q2-1}
# Insert your answer here
```

2.  Then give the commands to produce:

```{=html}
<!-- -->
```
(i) a vector containing the 200^th^, 400^th^, 600^th^, 800^th^ and 1000^th^ elements;

```{r Q2-2-i}
# Insert your answer here
```

(ii) a vector containing all elements except those from the 1^st^ to the 100^th^ element and those from the 900^th^ to the 1000^th^; [Only give the command, DO NOT print the resulting vector!]

```{r Q2-2-ii, eval = FALSE}
# Insert your answer here
# REMEMBER TO KEEP eval = FALSE to avoid printing the resulting vector
```

(iii) the number (or proportion) of elements between 6 and 9;

```{r Q2-2-iii}
# Insert your answer here
```

(iv) a random sample of size 500 with replacement from the elements of `x` which are less than 6.

```{r Q2-2-iv}
# Insert your answer here
```

\pagebreak

## 3. Short Mathematical Questions

1.  Leap years

  (a) Write an R function which accepts a list of years written as integers and return only the values in the original list that are divisible by 4.

```{r Q3-1-a} 
# Insert your answer here
```

  (b) A year is described as a leap year if (i) the year is divisible by 4 AND (ii) either year is not divisible by 100 OR it is divisible by 400. Using this definition of a leap year, write and R function which accepts a list of years written as integers and return only the values in the original list which are leap years.
```{r Q3-1-b} 
# Insert your answer here
```

  (c) Create a sequence including all years from 1515 to 2022 and write down how many of those years are leap years.
  
```{r Q3-1-c} 
# Insert your answer here
```

2.  Prime numbers

  (a) Write an R function primefind which returns `TRUE` if its integer argument is prime. Recall that a prime ia any number divisible only by itself and 1, and 1 is not a prime.

```{r Q3-2-a} 
# Insert your answer here
```

  (b) Using your previous function, create a new function which accepts a list of integers and returns only the values which are prime.
  
```{r Q3-2-b} 
# Insert your answer here
```

  (c) List all prime numbers between 1 and 300.

```{r Q3-2-c} 
# Insert your answer here
```

\pagebreak

## 4. Longer Mathematical Questions

1. The following R code generates a function which checks whether a positive integer is a cube number (i.e. a number for which the cube root is also an integer).

```{r Q4-1-fun}
cubetest<-function(x){
  b<-round(x^(1/3), digits=6)
  if(b%%1==0){return("Cube number")
  } else {
    return("Not a cube number")
  } 
}
```

  (i) Why might the function be necessary? 
  
  **HERE YOU CAN INSERT YOUR WRITTEN ANSWERS WITHOUT THE NEED OF A CHUNK**
    
  (ii) Write a function which has as its input a vector where each element is a positive integer, and which returns each element of the input vector which is a cube number.

```{r Q4-1-ii}
# Insert your answer here
```

2. Fermat’s theorem suggest, in part, that there are no positive integer solutons to the equation $X^3 + Y^3 = Z^3$. In other words, if $X, Y, Z \,\in \mathbb{Z}$, $X^3 + Y^3 = Z^3$ is never true.

  (i) Write a function with two inputs $X$ and $Y$ that can tell you whether the sum of $X^3$ and $Y^3$ is a cube number.
  
```{r Q4-2-i}
# Insert your answer here
```

  (ii) By using the functions you have already written, or otherwise, write code that proves that if $X\,\in\,\lbrace 1,\dots,100\rbrace$ and $Y\,\in\,\lbrace 1,\dots,100\rbrace$,there is no $Z \,\in\mathbb{Z}$ for which $X^3 + Y^3 = Z^3$.
  
```{r Q4-2-ii}
# Insert your answer here
```
  
3. Constructive criticism is possible even here. Identify up to two weaknesses in the code you have written for parts (a) and (b), and suggest a possible improvement. You do not need to explain exactly how such a solution would be coded. Consider, for instance, efficiency (could the same information be obtained with less computation) and robustness (could things go wrong, and how might these difficulties be avoided?)

*HERE YOU CAN INSERT YOUR WRITTEN ANSWERS WITHOUT THE NEED OF A CHUNK*