## Exercises

1. Write a function called pattern() that when given a character will print out the character in a pattern like this:

*
**
***
**
*

That is: a row of one, then a row of two, then a row of three, then a row of two, and finally a row of one.

The function should take one parameter called char. The default value of this parameter should be *. Typical examples of use should be as follows:

pattern()
## *
## **
## ***
## **
## *
pattern(char = "x")
## x
## xx
## xxx
## xx
## x
2. Write a function called charSquare() that when given two characters will use them to make a square like this:

****
a  a
a  a
****

That is: a row of four of one of the characters, then two rows that consist of the other character followed by two spaces followed by that other character, and finally a row of four of the first character.

The function should take two parameters called end and middle. The default value of end should should be * and the default value of middle should be x. Typical examples of use should be as follows:

charSquare()
## ****
## x  x
## x  x
## ****
charSquare(end = "z", middle = "%")
## zzzz
## %  %
## %  %
## zzzz
3. Write a function called reverse() that, given any vector, returns a vector with the elements in reverse order. It should take one parameter called vec. The default value of vec should be the vector c("Bob", "Marley"). Typical examples of use should be:

reverse()
## [1] "Marley" "Bob"
reverse(c(3,2,7,6))
## [1] 6 7 2 3

Hint: Recall how you can use sub-setting to reverse:

firstFiveLetters <- c("a", "b", "c", "d", "e")
firstFiveLetters[5:1]
## [1] "e" "d" "c" "b" "a"

You just need to figure out how to reverse vectors of arbitrary length.

4. A vector is said to be a palindrome if reversing its elements yields the same vector. Thus, c(3,1,3) is a palindrome, but c(3,1,4) is not a palindrome.

Write a function called isPalindrome() that, when given any vector, will return TRUE if the vector is a palindrome and FALSE if it is not a palindrome. The function should take a single parameter called vec, with no default value. Typical examples of use should be:

isPalindrome(vec = c("Bob", "Marley", "Bob"))
## [1] TRUE
isPalindrome(c(3,2,7,4,3))
## [1] FALSE

Hint: You already have the function reverse() from the previous Exercise. Use this function, along with the Boolean operator == and the all() function.

5. The eighteenth-century mathematician Leonhard Euler discovered that:

$\frac{\pi^2}{6} = \sum_{k=1}^{k=\infty} \frac{1}{k^2}.$ It follows that $\pi = \sqrt{\left(\sum_{k=1}^{k=\infty} \frac{6}{k^2}\right)}.$ Use this fact to write a function called eulerPI() that will approximate $$\pi$$. The function should take a single parameter n, which is the number of terms in the infinite series that are to be summed to make the approximation. The default value of n should be 10,000.

6. Consider the infinite series:

$\sum_{k=1}^{k=\infty} \frac{1}{k(k+1)}.$ Write a function called partialSum() that will compute the sum of the first $$n$$ terms of this series. The function should take a single parameter n, which is the number of terms in the series that are to be summed to make the approximation. The default value of n should be 10,000. Use the function to compute the sum of the first 10,000 terms and the sum of the first 100,000 terms. What number do you thnk the series converges to?