Homework 4: Atomic Vector Puzzles

Problem 1

First assigned all the variables to their letters. Then assigned z to each formula I needed to make, made sure I used markers like asterisks to create the math problem.

x <- 1.1
a <- 2.2
b <- 3.3

z <- x^(a^b)
print(z)
## [1] 3.61714
z <- (x^a)^b
print(z)
## [1] 1.997611
z <- 3*(x^3)+2*(x^2)+1
print(z)
## [1] 7.413

Problem 2

  1. Combined 2 vectors going forward and backwards
z <- c(1,2,3,4,5,6,7,8)
z <- c((seq(from=1, to=8)),(seq(from=7, to=1)))
print(z)
##  [1] 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
  1. Made sure R would only print 1-5, and wanted the numbers to repeat their number of times so made “times” the vector
z <- seq(from=1, to=5)
z <- rep(x=z, times=z)
print(z)
##  [1] 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
  1. Similar to b however we wanted the numbers to repeat number of times “backwards” so used “rev” command in the “times” command
z <- seq(from=5, to=1)
z <- rep(x=z, times=rev(z))
print(z)
##  [1] 5 4 4 3 3 3 2 2 2 2 1 1 1 1 1

Problem 3

First created the “runifs” to make random numbers for x and y. Then used polar coordinates conversion formula to define r and theta. Then combined those two things to make an actual set of coordinates.

x <- runif(1)
print(x)
## [1] 0.6134971
y <- runif(1)
print(y)
## [1] 0.9483262
r <- sqrt((x^2)+(y^2))
print(r)
## [1] 1.129469
theta <- atan(y/x)
print(theta)
## [1] 0.996585
coords <-c(r,theta)
print(coords)
## [1] 1.129469 0.996585

Problem 4

queue <- c("sheep", "fox", "owl", "ant")
print(queue)
## [1] "sheep" "fox"   "owl"   "ant"
# Serpent gets in line
queue <- c(queue, "serpent")
print(queue)
## [1] "sheep"   "fox"     "owl"     "ant"     "serpent"
# Sheep enters ark
queue <- c(queue[2:5])
print(queue)
## [1] "fox"     "owl"     "ant"     "serpent"
# Donkey talks his way to the front
queue <- c("donkey", queue[1:4])
print(queue)
## [1] "donkey"  "fox"     "owl"     "ant"     "serpent"
# Serpent gets impatient and leaves
queue <- c(queue[1:4])
print(queue)
## [1] "donkey" "fox"    "owl"    "ant"
# Owl gets bored and leaves
queue <- c(queue[1:2], queue[4])
print(queue)
## [1] "donkey" "fox"    "ant"
# Aphid cuts the ant
queue <- c(queue[1:2], "aphid", queue[3])
print(queue)
## [1] "donkey" "fox"    "aphid"  "ant"
# Define position of aphid in line
print(which("aphid" == queue))
## [1] 3

Problem 5

x <- 1:100

y <- which((a%%3!=0) & (a%%3!=0) & (a%%7!=0))
print(y)
## [1] 1