Homework Week1 (R)

Question 1

1 . Write a loop that calculates 12-factorial

Function

get.Factorial <- function(num){
  total <- 1
  
  # The double ||/&& form compares only one element from each side, 
  # while the single (|/&)form compares each element of each side.
  # The double form (&& or ||) is best used in if and the single form (& or |) is necessary for ifelse. 
  if(!is.numeric(num) || (sign(num)== -1)){
    print(sprintf("Sorry I can't calculate factorial of %s.", num))
    
  }else{
    if (num == 1 || num == 0){  # if Number is 0 or 1
      #total is set to 1 so no code is needed here.
    }else{
         for (n in 2:num){
          total <- total*(n)
        }
    }
    
    # Print output 
     cat( paste0("Factorial for " , num,"!"," ="),fill =FALSE, total ,"\n")
     return(total)
    
  }
    
} # End of Function

Calculation 12!

#Get user input
#num <- as.integer(readline(prompt="Show Me Factorial of Number:"))
num <- 12

#call Function to caculate 
Factorial <- get.Factorial(num)
## Factorial for 12! = 479001600
#Validating Answer with Machine 
print(paste("Calculated answer is " , identical(factorial(num),Factorial)))
## [1] "Calculated answer is  TRUE"

Question 2

2 . Create a Numeric vector that contains the sequence from 20 to 50 by 5

#using Seq from and to to build numeric Vector
HMVector <- seq(from = 20,to = 50,by = 5)
HMVector
## [1] 20 25 30 35 40 45 50
class(HMVector)
## [1] "numeric"

Question 3

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.

Formula Ref: The Quadratic Formula: For ax^2 + bx + c = 0, the values of x which are the solutions of the equation are given by: https://www.purplemath.com/modules/quadform.htm

Function

# Function Starts here. to solve the Quadaratic Equation 
# If users input is not Number, function give propoer message
# Functions resturns both vlaue with Equation 
Solve.EQ <- function(a , b , c){
  if(is.numeric(a) & is.numeric(b) & is.numeric(c)){
    
    #building Equation 
    meq <- paste0(a,"x^2 + (",b,"*X) + (" ,c ,") = 0")
    meq2 <- paste0(a,"x^2 + ",b,"*X + " ,c ," = 0")
    
    #...............solving Equation 1....................
    eq1 <- ( (-b) - sqrt(b^2-(4*a*c)) ) / 2*a
    
    #...............solving Equation 2....................
    eq2 <- ( (-b) + sqrt(b^2-(4*a*c)) ) / 2*a
    
    #Printing Equation and the solution is x = ?, x = ?.
    paste0("For Equation " , meq ," Then, expected solution is x = ", eq1, " and x = ",  eq2)
  }else
  {
     print(sprintf("Sorry we can't solve quadratic equation with a= %s, b = %s and c = %s.", a,b,c))
  }
}

Take User Input

# Making sure we start fresh 
rm(a,b,c) 
## Warning in rm(a, b, c): object 'a' not found
## Warning in rm(a, b, c): object 'b' not found
## Warning in rm(a, b, c): object 'c' not found
#user input
a <- as.integer(readline(prompt=" Enter value for a: (eg.1) "))
##  Enter value for a: (eg.1)
b <- as.integer(readline(prompt=" Enter value for b: (eg.3) "))
##  Enter value for b: (eg.3)
c <- as.integer(readline(prompt=" Enter value for c: (eg.-4) "))
##  Enter value for c: (eg.-4)
Solve.EQ(a ,b , c)
## [1] "For Equation NAx^2 + (NA*X) + (NA) = 0 Then, expected solution is x = NA and x = NA"

Some Other Function Test

#Some test with direct call 
Solve.EQ(a = 1 , b = 3 , c = -4)
## [1] "For Equation 1x^2 + (3*X) + (-4) = 0 Then, expected solution is x = -4 and x = 1"
Solve.EQ(1 , 3,  -4)
## [1] "For Equation 1x^2 + (3*X) + (-4) = 0 Then, expected solution is x = -4 and x = 1"
Solve.EQ(0 , 0,  0)
## [1] "For Equation 0x^2 + (0*X) + (0) = 0 Then, expected solution is x = 0 and x = 0"
#will result in Error 
Solve.EQ("a" , 0,  0)
## [1] "Sorry we can't solve quadratic equation with a= a, b = 0 and c = 0."