Chapter 3 Excercise 2

h(x,n) = 1 + x + x^2 +… x^n

n=1 implies h(x,1) = 1

n=2 implies h(x,2) = 1 + x = h(x,1) + x

n=3 implies h(x,3) = 1 + x + x^2 = h(x,2) + x^2

n=4 implies h(x,4) = 1 + x + x^2 + x^3 = h(x,3) + x^3

So, we see a pattern and that is how we construct our for loop

Below, we can modify the different parameters

h<-1
n<-1
x<-1
for (i in 1:n){
  h <- h + x^i
}

print(h)
## [1] 2
h<-1
n<-2
x<-1
for (i in 1:n){
  h <- h + x^i
}

print(h)
## [1] 3
h<-1
n<-10
x<-1
for (i in 1:n){
  h <- h + x^i
}

print(h)
## [1] 11

Chapter 3 Excercise 7

In this case, we let x be the vector of the numbers 1 to 14. Then, we try it for a different vector not in order. It looks good to me.

x <- 1:14
n<- sum(x[seq(0, length(x), 3)])
n 
## [1] 30
x<- c(1,12,4,10,11,6)
n<- sum(x[seq(0, length(x), 3)])
n 
## [1] 10

Chapter 3 Excercise 10

examp <- c(10,11,3000,4,6,84,-13)
min_func_pp <- function(x){
  x.min <- x[1]
  for (i in 1:length(x)){
    if (x[i] < x.min) {
      x.min <- x[i] 
    }
  }
  print(x.min)
}

min_func_pp(examp)
## [1] -13

Chapter 3 Excercise 12

I don’t think the directions in the question define all the rules of craps.

David says “You don’t want to roll a 7 but if the pass line is off then you would win that bet, also you dont want to roll a 2,3,or 5 when a point isn’t established”.

I don’t know what this exactly means, but he wins a lot at craps.

set.seed(1234)

craps <- function(){
  x <- sum(ceiling(6*runif(2)))
  if (x== 7 | x == 11) {print(paste("winner from first roll, you rolled a", x))}
  else {
    print(paste("You didn't immediately win but your original roll is",x))
    dice <- sum(ceiling(6*runif(2)))
  while (dice != x & dice != 7 & dice != 11)
 {
    print(dice)
dice <- sum(ceiling(6*runif(2)))
  }
  if (dice == x) print(paste("Your roll matched your first roll, congrats! Are you cheating? You rolled", dice))
  else print(paste("The house almost always wins you should know that you loser you rolled", dice))
}}
craps()
## [1] "You didn't immediately win but your original roll is 5"
## [1] 8
## [1] 10
## [1] 3
## [1] 8
## [1] 9
## [1] 8
## [1] 8
## [1] 4
## [1] 4
## [1] 4
## [1] 2
## [1] "The house almost always wins you should know that you loser you rolled 7"

Chapter 3 Excercise 13

t <- seq(0, 10, by = 0.0001)
x <- sqrt(t) *cos(2*pi*t)
y <- sqrt(t) *sin(2*pi*t)
plot(main = "Polar Coordinate Plot", x, y, cex=.1)