I am an optional attendee of this this R Bridge program.
Write a loop that calculates 12-factorial
recurse <- function(x){
if(x==1)
{
#stop calling yourself!!
return(1)
}
else
{
#return yourself, times 1 minus yourself.
return(x * recurse(x-1))
}
}
answer <- recurse(12)
validation <- factorial(12)
sprintf("12! == %s == %s" , answer, validation)
## [1] "12! == 479001600 == 479001600"
Show how to create a numeric vector that contains the sequence from 20 to 50 by 5
my.range <- seq(25,50, by=5)
my.range
## [1] 25 30 35 40 45 50
Create the function quad that takes a trio of input numbers a, b, and c and solve the quadratic equation. The function should print as output the two solutions. Please run and test your answer for (1,2,1), (1,6,5) and (1,1,1).
# discriminant explained here.
# https://dk81.github.io/dkmathstats_site/rmath-quad-formula-r.html
quad <- function(a,b,c){
disc <- (b^2) - (4*a*c)
if (disc < 0) {
return("No Real Roots")
}
else if (disc > 0) {
plus <- (-b + sqrt(disc)) / (2*a)
minus <- (-b - sqrt(disc)) / (2*a)
return(sprintf("[%s,%s]", plus, minus))
}
else if ( disc == 0)
x <- (-b) / (2*a)
return(sprintf("Only One Root: [%s]", x))
}
sprintf("quad(1,2,1) = %s", quad(1,2,1))
## [1] "quad(1,2,1) = Only One Root: [-1]"
sprintf("quad(1,6,5) = %s", quad(1,6,5))
## [1] "quad(1,6,5) = [-1,-5]"
sprintf("quad(1,1,1) = %s", quad(1,1,1))
## [1] "quad(1,1,1) = No Real Roots"