Week4Solutions.Rmd
First, core solutions are provided. Then some interesting student alternative solutions are examined.
i <- 1
fact <- 1
MAX_FACT = 12
while (i <= MAX_FACT)
{
fact <- i * fact
i <- i + 1
}
fact
## [1] 479001600
seq(20,50,5)
## [1] 20 25 30 35 40 45 50
a <- 1
b <- -4
c <- 4
x1 <- (-1 * b + (( b ^ 2 - (4 * a * c))^.5)) / (2 * a)
x2 <- (-1 * b - (( b ^ 2 - (4 * a * c))^.5)) / (2 * a)
print(c(x1, x2))
## [1] 2 2
Here are some interesting alternative solutions proposed by students. Some of these were improvements to my provided solutions above.
Here is Mohamed’s solution to #1. I especially like that (1) he wraps the code in a function; and (2) he tests the function with a number of values. Also, he identifies a “base R” function that does the same job, then uses this to verify his results. Nice!
i=1
myfact=1
for (i in 1:12)
{ myfact=myfact*i
i<-i+1}
myfact
## [1] 479001600
# Testing using the built-in factorial function
factorial(12)
## [1] 479001600
# Or as function, it can be written a below for any integer that is greater than 0
myfactorial<- function(n) {
if (n<0) { print("Please enter positive values")
return()
}
i=1
myfact=1
if (n==0) { return(myfact)}
for (i in 1:n)
{ myfact=myfact*i
i<-i+1}
return(myfact)
}
#testing
myfactorial(12)
## [1] 479001600
myfactorial(3)
## [1] 6
myfactorial(10)
## [1] 3628800
myfactorial(5)
## [1] 120
#Testing using the built-in factorial function
factorial(12)
## [1] 479001600
For #2, Vuthy provided the same solution that I did using the seq() function; he also provided this fun, mathy alternative approach:
vec1 <- c(4:10)
#Vectorize multiplication operation
vec2 <- vec1*5
vec2
## [1] 20 25 30 35 40 45 50
Here is another cool and interesting alternative solution to #2, posted by Lalani:
nvect<-20:50
print(nvect[nvect %% 5 == 0])
## [1] 20 25 30 35 40 45 50
For #3, Sreejaya wrapped the code into a function. Notice how Sreejaya handles the issue that a function can return only one thing and this function needs to return two values.
quadraticfunc <-function(a,b,c)
{
x1 <- ((-b) + sqrt(b^2-(4*a*c))/2*a)
x2 <- ((-b) - sqrt(b^2-(4*a*c))/2*a)
sqrtlist <- list(sqrtlistval = c(x1, x2))
sqrtlist$sqrtlistval
}
quadraticfunc(4,9,1)
## [1] 7.124515 -25.124515
The last executable statement in the function is returned, but I would have preferred using a return statement to make it explicit:
return(sqrtlist$sqrtlistval)
There is a bigger problem, that Sreejaya didn’t catch, and I didn’t catch when I graded it!
What happens when we pass the function instead quadraticfunc(1,-4,4). That is, (x-2)*(x-2)?
The lesson learned here is that its important to always test our code against known results.
Here is Yee’s solution to #3, where he adds support for complex roots.
a <- 1
b <- 6
c <- 87
x<-0
input<-c(a,b,c)
if ((input[2]^2-4*input[1]*input[3])>0) {
x[1] <- (-input[2]+(input[2]^2-4*input[1]*input[3])^0.5)/(2*input[1])
x[2] <- (-input[2]-(input[2]^2-4*input[1]*input[3])^0.5)/(2*input[1])
} else {
x[1] <- complex(real=-input[2]/(2*input[1]),imaginary=(abs(input[2]^2-4*input[1]*input[3])^0.5)/(2*input[1]))
x[2] <- complex(real=-input[2]/(2*input[1]),imaginary=-(abs(input[2]^2-4*input[1]*input[3])^0.5)/(2*input[1]))
}
print(paste0("First Root is ", x[1]))
## [1] "First Root is -3+8.83176086632785i"
print(paste0("Second Root is ", x[2]))
## [1] "Second Root is -3-8.83176086632785i"
To consider: how would you wrap Yee’s code in a function?
And finally, Vuthy’s answer to #3 doesn’t handle complex roots, but does provide graceful error handling:
quadratic_formula <- function(a,b,c) {
if((b^2)-(4*a*c)>=0)
{
q_p_x <-((-1*b)+sqrt((b^2)-(4*a*c)))/(2*a)
q_n_x <- ((-1*b)-sqrt((b^2)-(4*a*c)))/(2*a)
quadratic <- c(q_p_x, q_n_x)
quadratic
}
else cat("Can't Compute the Sqrt of a negative number")
}
quadratic_formula(1, -3, -4)
## [1] 4 -1
quadratic_formula(1, 3, 4)
## Can't Compute the Sqrt of a negative number