Pythagorean theorem

  1. Make a function to compute the hypotenuse of a rectangle triangle from the other sides.

  2. Make a function that accepts two sides of a rectangle triangle (optional parameters a, b, c, where a stands for the hypotenuse) and return a named vector with all three sides. Suggestions: if, is.null

Odd or even

  1. Make a function returning whether a given number is odd or even. Suggestion: if

  2. Make a function returning whether each number in a given vector is odd or even. Suggestion: ifelse

Rays

Make an script to draw the following graphic:

Suggestions: plot(0,0, type="n") to start, abline(a=0, b=tan(angle*pi/180), col="orange") and abline(v=0, h=0, col="red") for lines.

Guess the number game

Build an script to play this game:

Suggestion: readline (not to be confused with readLines), while, if

Collatz sequence

The Collatz sequence is a sequence with starting point at any given positive integer and the remaining points defined by:

\(a_{i+1} = \begin{cases} \frac{a_i}{2} &\text{if } a_i \text{ is even}\\[4px] 3a_i+1 & \text{if } a_i\text{ is odd}\end{cases}\)

Every Collatz sequence eventually reaches 1, although this result is still to be proven (Collatz conjecture).

  1. Write an R function that returns the Collatz sequence for a given positive integer. Suggestions: while, if

  2. It is also possible generalize Collatz sequences starting with any integer. Not all of those sequences reach 1 but all sequences eventually reach a cycle, that is, they start repeating them selves (although this fact is still also unproven). Modify previous function to stop when it repeats a number. Suggestion: Use %in% to check whether an element is in a vector.

lapply and sapply

  1. Compute the mean of each variable in dataset mtcars

  2. Compute a summary of each variable in dataset mtcars