1- A loop that calculates 12-factorial
x = 12
k = 1
while(x > 0){
k = k * x
x = x - 1
}
k
## [1] 479001600
2- A numeric vector that contains the sequence from 20 to 50 by 5
vec = seq(20,50,5)
vec
## [1] 20 25 30 35 40 45 50
3- A root finder function with 3 inputs for a quaratic equation
factorial = function(a,b,c){
dis = b^2-4*a*c
if(dis < 0) {
print("no real solution")
}else if(dis == 0){
print(c(NA, -b/(2*a)))
}else{
xa = (-b + sqrt(dis)) / (2*a)
xb = (-b - sqrt(dis)) / (2*a)
print(c(xa, xb))
}
}
factorial(1,-1,-6)
## [1] 3 -2
factorial(1,4,4)
## [1] NA -2
factorial(1,3,4)
## [1] "no real solution"