- Write a loop that calculates 12!
x<-1
for(i in 1:12){
x <-x*(13-i)
}
print(x)
## [1] 479001600
- Show how to create a numeric vector that contains the sequence from 20 to 50 by 5.
n<-(5*(4:10))
print(n)
## [1] 20 25 30 35 40 45 50
is.numeric(n)
## [1] TRUE
- Create the function “factorial” 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.
factorial<-function(a,b,c){
rt<-b^2-(4*a*c)
sub=ifelse(rt<0,sqrt(rt+0i),sqrt(rt))
c((-b+sub)/(2*a),(-b-sub)/(2*a))
}
factorial(1,0,-9)
## [1] 3 -3
factorial(1,0,9)
## [1] 0+3i 0-3i