- Write a loop that calculates 12-factorial
factorial <- function(num){
total = num
while (num>1){
total = total*(num-1)
num = num - 1
}
return(total)
}
factorial(12)
## [1] 479001600
- Show how to create a numeric vector that contains the sequence from 20 to 50 by 5.
seq(20,50,5)
## [1] 20 25 30 35 40 45 50
- Create the function “quadratic” 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.
quadratic <- function(a,b,c){
delta = b^2-4*a*c
if(delta > 0){
x1 = (-b-sqrt(delta))/(2*a)
x2 = (-b+sqrt(delta))/(2*a)
roots = c(x1,x2)
}
else if(delta == 0){
x = -b/(2*a)
roots = c(x)
}
else{
roots = "No real Roots"
}
return(roots)
}
quadratic(1,6,5)
## [1] -5 -1