# R Bridge Week 1 Assignment
# SPS Cuny - Bridge Program
# Spring 2017
# Duubar E. Villalobos Jimenez
# mydvtech@gmail.com
# Please create the following exercises in .rmd format, publish to rpub and submit both the .rmd file and the rpub link.
# 1. Write a loop that calculates 12-factorial.
MyFactorialFunction <- function(MyFactorialInput) {
MyFactorial <- 1
for (i in 1:MyFactorialInput)
{
MyFactorial <- MyFactorial * i
}
return(MyFactorial)
}
x <- 12
MyFactorialFunction(x) # 12! Should return 479001600
## [1] 479001600
#2. Show how to create a numeric vector that contains the sequence from 20 to 50 by 5
MyNumericVector <- seq(20, 50, by = 5 )
MyNumericVector
## [1] 20 25 30 35 40 45 50
# 3. 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.
MyFactorial <- function (a, b, c) {
MyDiscriminant <- b ^ 2 - 4 * a * c # Find the Discriminant
if (MyDiscriminant >= 0)
{
MySol1 <- (-b + sqrt(MyDiscriminant)) / 2 * a # Find First Solution
MySol2 <- (-b - sqrt(MyDiscriminant)) / 2 * a # Find Second Solution
}
else # Working Imaginary Solutions
{
print(" ***** WARNING THE SOLUTION TO THIS EQUATION IS IN THE COMPLEX GROUP OF NUMBERS ***** ")
MySol1 <- paste("(",-b ,"+", sqrt(-MyDiscriminant),"i)","/(", 2 * a,")") # Imaginary Solutions
MySol2 <- paste("(",-b ,"-", sqrt(-MyDiscriminant),"i)","/(", 2 * a,")") # Imaginary Solutions
if (b == 0) # Simplifying Fractions
{
MySol1 <- paste(sqrt(-MyDiscriminant),"i/(", 2 * a,")") # Imaginary Solutions
MySol2 <- paste(-sqrt(-MyDiscriminant),"i/(", 2 * a,")") # Imaginary Solutions
if (sqrt(-MyDiscriminant) == a)
{
MySol1 <- paste("i","/", 2) # Imaginary Solutions
MySol2 <- paste("-i","/", 2) # Imaginary Solutions
if (sqrt(-MyDiscriminant) == 2)
{
MySol1 <- paste("i") # Imaginary Solutions
MySol2 <- paste("-i") # Imaginary Solutions
}
}
if (sqrt(-MyDiscriminant) == 2)
{
MySol1 <- paste("i","/", a) # Imaginary Solutions
MySol2 <- paste("-i","/", a) # Imaginary Solutions
if (a == 1)
{
MySol1 <- paste("i") # Imaginary Solutions
MySol2 <- paste("-i") # Imaginary Solutions
}
}
}
}
if (b >= 0)
{
b = paste("+",b)
}
if (c >= 0)
{
c = paste("+",c)
}
# Final Print outs
OriginalEquation <- paste(a,"x^2",b,"x",c, "= 0")
print(paste("The original Equation is: ", OriginalEquation))
print(paste("The first solution is: x1 = ", MySol1))
print(paste("The second solution is: x2 = ", MySol2))
if (is.numeric(MySol1) & is.numeric(MySol2))
{
MyEquation <- paste("(x +", (-1 * MySol1), ")(x +", (-1 *MySol2), ") = 0")
if (MySol1 == MySol2)
{
MyEquation <- paste("(x", -1 * MySol1, ")^2 = 0")
}
}
else
{
MyEquation <- paste("(x +", ( MySol1), ")(x +", ( MySol2), ") = 0")
if (MySol1 == MySol2)
{
MyEquation <- paste("(x", MySol1, ")^2 = 0")
}
}
print(paste("The factorized equation will be: ", MyEquation))
# MySolutionsFrame <- data.frame("Original" = OriginalEquation, "x1" = MySol2, "x2" = MySol1, "Equation" = MyEquation)
# MySolutionsFrame
}
MyFactorial(10,21,-27)
## [1] "The original Equation is: 10 x^2 + 21 x -27 = 0"
## [1] "The first solution is: x1 = 90"
## [1] "The second solution is: x2 = -300"
## [1] "The factorized equation will be: (x + -90 )(x + 300 ) = 0"