Exercise 1

Write a set of conditional(s) that satisfies the following requirements:
* If x is greater than 3 and y is less than or equal to 3 then print “Hello world!”
* Otherwise if x is greater than 3 print “Hello world!”
* If x is less than or equal to 3 then print “Something else …”
* Stop execution if x is odd and y is even and report an error, don’t print any of the text strings above.

Here is my code before testing the conditionals:

if (x %% 2 != 0 & y %% 2 == 0) {
  stop("Error: x is odd and y is even")
} else if (x > 3  & y <= 3) {
  print("Hello world")
} else if (x > 3) {
  print("Hello World")
} else if (x <= 3) {
  print("Something else...")
}

Testing the code with x = 1 and y = 2:

x = 1
y = 2
if (x %% 2 != 0 & y %% 2 == 0) {
  stop("Error: x is odd and y is even")
} else if (x > 3  & y <= 3) {
  print("Hello world")
} else if (x > 3) {
  print("Hello World")
} else if (x <= 3) {
  print("Something else...")
}
## Error in eval(expr, envir, enclos): Error: x is odd and y is even

Here you get the error that x is odd and y is even which is what the stop condition is for the code.

Next, we are going to test when x = 1 and y = 3:

x = 1
y = 3
if (x %% 2 != 0 & y %% 2 == 0) {
  stop("Error: x is odd and y is even")
} else if (x > 3  & y <= 3) {
  print("Hello world")
} else if (x > 3) {
  print("Hello World")
} else if (x <= 3) {
  print("Something else...")
}
## [1] "Something else..."

Since x is not greater than 3, the code prints out Something else because the if statement was read as false for the first three conditionals and the last else if statement is read as true.

The last conditional test is looking at x = 3 and y = 3:

x = 3
y = 3
if (x %% 2 != 0 & y %% 2 == 0) {
  stop("Error: x is odd and y is even")
} else if (x > 3  & y <= 3) {
  print("Hello world")
} else if (x > 3) {
  print("Hello World")
} else if (x <= 3) {
  print("Something else...")
}
## [1] "Something else..."

Again, both x and y are odd integers and since x is not greater than 3, only the last else if statement is evaluated as true, which is why something else was printed.


Exercise 2

Write a set of conditional(s) in R to solve equations of the form ax^2 + bx + c, which accounts for possible errors that might occurr in the calculation (you may use if, else if, else, stop, stopifnot) Test out your code with:
1. a=1, b=0 and c=1
2. a=1, b=0 and c=-1
3. a=9, b=4 and c=1
4. a=0, b=3 and c=3
5. a=1, b=-4 and c=4

Test with a = 1, b = 0, c = 1:

a = 1
b = 0
c = 1
if (a == 0 & b == 0 & c == 0){
  stop("Error: no values were given")
}
if (a != 0){
  d = (b^2 - (4*a*c))
  if (d < 0){
    print("There are no x intercepts")
  }
  if (d >= 0){
    x_neg = ((-1*b - sqrt(b^2 -(4*a*c))) / (2*a))
    x_pos = ((-1*b + sqrt(b^2 -(4*a*c))) / (2*a)) 
    if (b != 0 & b > 0 & is.nan(x_pos) == FALSE & is.nan(x_neg) == FALSE){
      print(paste("The x values for the equation", a,"x^2 + ", b, "x + ",c, "is 
                  x =", x_neg, ",", x_pos))
    }else if (b != 0 & b < 0 & is.nan(x_pos) == FALSE & is.nan(x_neg) == FALSE 
              & d != 0){print(paste("The x intercept for the equation", a,
                                    "x\u00B2 ", b, "x + ",c, "is x =", x_neg,
                                    ",", x_pos))
    }else if (b == 0 & is.nan(x_pos) == FALSE & is.nan(x_neg) == FALSE & d != 0)
      {print(paste("The x intercepts for the equation", a,"x\u00B2+",c,"is x =", 
                   x_pos, ",",x_neg))
    }else if (b == 0 & is.nan(x_pos) == FALSE & is.nan(x_neg) == FALSE & d == 0)
      {print(paste("The x intercepts for the equation", a,"x\u00B2+",c,"is x =", 
                   x_pos))
    }else if (b != 0 & b < 0 & is.nan(x_pos) == FALSE & is.nan(x_neg) == FALSE 
              & d == 0){
      print(paste("The x intercept for the equation", a,"x\u00B2 ", b, "x + ",c, 
                  "is x =", x_neg))
    }
  }
}
## [1] "There are no x intercepts"
if (a == 0){
  x_int = (-1*c) / b
  print(paste("The x intercept is x =", x_int))
}

Test with a = 1, b = 0, c = -1:

a = 1
b = 0
c = -1
if (a == 0 & b == 0 & c == 0){
  stop("Error: no values were given")
}
if (a != 0){
  d = (b^2 - (4*a*c))
  if (d < 0){
    print("There are no x intercepts")
  }
  if (d >= 0){
    x_neg = ((-1*b - sqrt(b^2 -(4*a*c))) / (2*a))
    x_pos = ((-1*b + sqrt(b^2 -(4*a*c))) / (2*a)) 
    if (b != 0 & b > 0 & is.nan(x_pos) == FALSE & is.nan(x_neg) == FALSE){
      print(paste("The x values for the equation", a,"x^2 + ", b, "x + ",c, "is 
                  x =", x_neg, ",", x_pos))
    }else if (b != 0 & b < 0 & is.nan(x_pos) == FALSE & is.nan(x_neg) == FALSE 
              & d != 0){
      print(paste("The x intercept for the equation", a,"x\u00B2 ", b, "x + ",c, 
                  "is x =", x_neg, ",", x_pos))
    }else if (b == 0 & is.nan(x_pos) == FALSE & is.nan(x_neg) == FALSE & d != 0)
      {print(paste("The x intercepts for the equation", a,"x\u00B2+",c,"is x =", 
                   x_pos, ",",x_neg))
    }else if (b == 0 & is.nan(x_pos) == FALSE & is.nan(x_neg) == FALSE & d == 0)
      {print(paste("The x intercepts for the equation", a,"x\u00B2+",c,"is x =",
                   x_pos))
    }else if (b != 0 & b < 0 & is.nan(x_pos) == FALSE & is.nan(x_neg) == FALSE 
              & d == 0){
      print(paste("The x intercept for the equation", a,"x\u00B2 ", b, "x + ",c, 
                  "is x =", x_neg))
    }
  }
}
## [1] "The x intercepts for the equation 1 x²+ -1 is x = 1 , -1"
if (a == 0){
  x_int = (-1*c) / b
  print(paste("The x intercept is x =", x_int))
}

Test with a = 9, b = 4, c = 1:

a = 9
b = 4
c = 1
if (a == 0 & b == 0 & c == 0){
  stop("Error: no values were given")
}
if (a != 0){
  d = (b^2 - (4*a*c))
  if (d < 0){
    print("There are no x intercepts")
  }
  if (d >= 0){
    x_neg = ((-1*b - sqrt(b^2 -(4*a*c))) / (2*a))
    x_pos = ((-1*b + sqrt(b^2 -(4*a*c))) / (2*a)) 
    if (b != 0 & b > 0 & is.nan(x_pos) == FALSE & is.nan(x_neg) == FALSE){
      print(paste("The x values for the equation", a,"x^2 + ", b, "x + ",c, "is 
                  x =", x_neg, ",", x_pos))
    }else if (b != 0 & b < 0 & is.nan(x_pos) == FALSE & is.nan(x_neg) == FALSE 
              & d != 0){print(paste("The x intercept for the equation", a,
                                    "x\u00B2 ", b, "x + ",c, "is x =", x_neg,
                                    ",", x_pos))
    }else if (b == 0 & is.nan(x_pos) == FALSE & is.nan(x_neg) == FALSE & d != 0)
      {print(paste("The x intercepts for the equation", a,"x\u00B2+",c,"is x =",
                   x_pos, ",",x_neg))
    }else if (b == 0 & is.nan(x_pos) == FALSE & is.nan(x_neg) == FALSE & d == 0)
      {print(paste("The x intercepts for the equation", a,"x\u00B2+",c,"is 
                   x =", x_pos))
    }else if (b != 0 & b < 0 & is.nan(x_pos) == FALSE & is.nan(x_neg) == FALSE &
              d == 0){print(paste("The x intercept for the equation", a,
                                  "x\u00B2 ", b, "x + ",c, "is x =", x_neg))
    }
  }
}
## [1] "There are no x intercepts"
if (a == 0){
  x_int = (-1*c) / b
  print(paste("The x intercept is x =", x_int))
}

Test with a = 0, b = 3, c = 3:

a = 0
b = 3
c = 3
if (a == 0 & b == 0 & c == 0){
  stop("Error: no values were given")
}
if (a != 0){
  d = (b^2 - (4*a*c))
  if (d < 0){
    print("There are no x intercepts")
  }
  if (d >= 0){
    x_neg = ((-1*b - sqrt(b^2 -(4*a*c))) / (2*a))
    x_pos = ((-1*b + sqrt(b^2 -(4*a*c))) / (2*a)) 
    if (b != 0 & b > 0 & is.nan(x_pos) == FALSE & is.nan(x_neg) == FALSE){
      print(paste("The x values for the equation", a,"x^2 + ", b, "x + ",c, "is 
                  x =", x_neg, ",", x_pos))
    }else if (b != 0 & b < 0 & is.nan(x_pos) == FALSE & is.nan(x_neg) == FALSE 
              & d != 0){
      print(paste("The x intercept for the equation", a,"x\u00B2 ", b, "x + ",c, 
                  "is x =", x_neg, ",", x_pos))
    }else if (b == 0 & is.nan(x_pos) == FALSE & is.nan(x_neg) == FALSE & d != 0)
      {print(paste("The x intercepts for the equation", a,"x\u00B2+",c,"is x =", 
                   x_pos, ",",x_neg))
    }else if (b == 0 & is.nan(x_pos) == FALSE & is.nan(x_neg) == FALSE & d == 0)
      {print(paste("The x intercepts for the equation", a,"x\u00B2+",c,"is x =", 
                   x_pos))
    }else if (b != 0 & b < 0 & is.nan(x_pos) == FALSE & is.nan(x_neg) == FALSE & 
              d == 0){print(paste("The x intercept for the equation", a,
                                  "x\u00B2 ", b, "x + ",c, "is x =", x_neg))
    }
  }
}
if (a == 0){
  x_int = (-1*c) / b
  print(paste("The x intercept is x =", x_int))
}
## [1] "The x intercept is x = -1"

Test with a = 1, b = -4, c =4:

a = 1
b = -4
c = 4
if (a == 0 & b == 0 & c == 0){
  stop("Error: no values were given")
}
if (a != 0){
  d = (b^2 - (4*a*c))
  if (d < 0){
    print("There are no x intercepts")
  }
  if (d >= 0){
    x_neg = ((-1*b - sqrt(b^2 -(4*a*c))) / (2*a))
    x_pos = ((-1*b + sqrt(b^2 -(4*a*c))) / (2*a)) 
    if (b != 0 & b > 0 & is.nan(x_pos) == FALSE & is.nan(x_neg) == FALSE){
      print(paste("The x values for the equation", a,"x^2 + ", b, "x + ",c, "is 
                  x =", x_neg, ",", x_pos))
    }else if (b != 0 & b < 0 & is.nan(x_pos) == FALSE & is.nan(x_neg) == FALSE 
              & d != 0){
      print(paste("The x intercept for the equation", a,"x\u00B2 ", b, "x + ",c,
                  "is x =", x_neg, ",", x_pos))
    }else if (b == 0 & is.nan(x_pos) == FALSE & is.nan(x_neg) == FALSE & d != 0)
      {print(paste("The x intercepts for the equation", a,"x\u00B2+",c,"is x =", 
                   x_pos, ",",x_neg))
    }else if (b == 0 & is.nan(x_pos) == FALSE & is.nan(x_neg) == FALSE & d == 0)
      {print(paste("The x intercepts for the equation", a,"x\u00B2+",c,"is x =",
                   x_pos))
    }else if (b != 0 & b < 0 & is.nan(x_pos) == FALSE & is.nan(x_neg) == FALSE 
              & d == 0){
      print(paste("The x intercept for the equation", a,"x\u00B2 ", b, "x + ",c,
                  "is x =", x_neg))
    }
  }
}
## [1] "The x intercept for the equation 1 x²  -4 x +  4 is x = 2"
if (a == 0){
  x_int = (-1*c) / b
  print(paste("The x intercept is x =", x_int))
}

Exercise 3

Below is a vector containing all prime numbers between 2 and 100:
primes = c( 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61,  67, 71, 73, 79, 83, 89, 97)
If you were given the vector x = c(3,4,12,19,23,51,61,63,78), write the R code necessary to print only the values of x that are not prime
(without using subsetting or the %in% operator). Your code should use nested loops to iterate through the vector of primes and x.

primes = c(2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 
           67, 71, 73, 79, 83, 89, 97)
x = c(3, 4, 12, 19, 23, 51, 61, 63, 78)
i = 1
j = 1
while (i <= length(x)){
  while (j <= length(primes)){
    if (x[i] == primes[j]){
      print(x[i])
    }
    j = j + 1
  }
  j = 1
  i = i + 1
}
## [1] 3
## [1] 19
## [1] 23
## [1] 61