Function fib()

Introduction

The Fibonacci sequence is the sequence \(\{F_n\colon n \geq 1\}\) of numbers defined by \(F_1 = 0, F_2 = 1\) and \(F_n = F_{n-2} + F_{n-1}\) for \(n\geq 3\). For example, the first seven Fibonacci numbers are \[\begin{align*} F_1 &= 0\\ F_2 &= 1\\ F_3 &= 0+1 = 1\\ F_4 &= 1 + 1 = 2\\ F_5 &= 1 + 2 = 3\\ F_6 &= 2 + 3 = 5\\ F_7 &= 3 + 5 = 8\\ \end{align*}\]

Part a

fib()

Write a function, fib(), which uses a for loop. Function fib() should take one argument, n, and return the first n Fibonacci numbers in a vector. See below for some examples.

fib(n = 1)
fib(n = 2)
fib(n = 7)
fib(n = 10)
[1] 0
[1] 0 1
[1] 0 1 1 2 3 5 8
 [1]  0  1  1  2  3  5  8 13 21 34

Hints

  • Assume the user will enter a value of \(n \geq 3\), try and get the function working under this set-up.

  • Use an if-else statement to handle what to do if \(n = 1\) or \(n = 2\).

  • Recall vector subsetting:

# generate 6 binomial random variables
set.seed(4839089)
x <- rbinom(n = 6, size = 10, prob = 0.5)
x

x[1:1]
x[1:5]
x[-1]
[1] 6 6 6 3 4 4
[1] 6
[1] 6 6 6 3 4
[1] 6 6 3 4 4

Part b

fib_1()

Modify fib() so that it uses a while loop instead of a for loop. Call this new function fib_1(). Test that your function works with the following examples below.

fib_1(n = 1)
fib_1(n = 2)
fib_1(n = 7)
fib_1(n = 10)
[1] 0
[1] 0 1
[1] 0 1 1 2 3 5 8
 [1]  0  1  1  2  3  5  8 13 21 34

Hints

  • Recall the while loop structure:
while (condition) {
  # code to iterate
}
  • increment your counter

Function all.primes()

Introduction

Your goal is to write a function all.primes() that will return all possible prime numbers among the first n positive integers. For example, among the integers 1 through 10, there are prime numbers: 2, 3, 5, 7. Recall that a prime number is a whole number greater than 1 whose only factors are 1 and itself.

To get started, proceed to step 1.

Step 1

is.prime()

R has built-in functions that perform logical tests. For example, is.numeric() and is.character() performs a logical test if an object is numeric and if an object is of type character, respectively. Write a function called is.prime() that tests if a numeric vector of length 1 is a prime number or not. The function should return a single TRUE or FALSE value. See below for some examples.

is.prime(x = 2)
is.prime(x = 10)
is.prime(x = 13)
[1] TRUE
[1] FALSE
[1] TRUE

Hints

  • How do you know that 13 is a prime number?

  • Take a look at the modulo operator: %%.

  • What do you notice in

13 %% (1:13)
14 %% (1:14)

Step 2

all.primes()

Write a function all.primes() that has one argument, n, and returns all possible prime numbers among the first n positive integers. See below for some examples.

all.primes(n = 10)
all.primes(n = 21)
all.primes(n = 2)
[1] 2 3 5 7
[1]  2  3  5  7 11 13 17 19
[1] 2

Hints

  • Use a while loop that iterates through the first n positive integers.

  • In each iteration, test if the number is prime with your function in step 1: is.prime().

  • Don’t forget to set-up your output vector before the loop begins. This is what all.primes() will return.