[Workspace loaded from ~/.RData]

R Programming - Bridge Workshop Week 1 Assignment

Ayala Cohen

Problem 1. Write a loop that calculates 12-factorial.

12! = 12 * 11 * 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 = 479,001,600

This code loop calculates factorial of 12 only, but no other numbers

factorial12 <- 1 for (i in 12:1) { + factorial12 = factorial12 * i + }

factorial12 [1] 479001600

This function with code loop prompts for an input and then calculates the factorial of any input number.

factorialf = function(num) { + x <- 1 + num <- as.integer(readline(prompt=“Enter a number:”)) + if(num < 0) { + print(“Sorry, factorial does not exist for negative numbers”) + } else if(num == 0) { + print(“The factorial of 0 is 1”) + } else { + for (i in 2:num) { + x <- x * i + } + return(x) + } + }

factorialf() Enter a number: 12 [1] 479001600 factorialf() Enter a number: 5 [1] 120 factorialf() Enter a number: 0 [1] “The factorial of 0 is 1” factorialf() Enter a number: -7 [1] “Sorry, factorial does not exist for negative numbers”

R Programming - Bridge Workshop Week 1 Assignment

Ayala Cohen

Problem 2. Show how to create a numeric vector that contains the sequence from 20 to 50 by 5.

numvec <- seq(from = 20, to = 50, by = 5)

numvec [1] 20 25 30 35 40 45 50

R Bridge Week 1 Assignment

Ayala Cohen

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.

factorial = function() + { + a <- readline(prompt=“Enter the coefficient of the x^2 term from your quadratic equation:”) + b <- readline(prompt=“Enter the coefficient of the x term from your quadratic equation:”) + c <- readline(prompt=“Enter the constant from your quadratic equation:”) + quadformula1 <- (-b + sqrt( (b^2) - (4ac) )) / (2a) + quadformula2 <- (-b - sqrt( (b^2) - (4ac) )) / (2a) + listboth <- list(quadformula1, quadformula2) + names(listboth) <- c(“quadratic equation solution 1”, “quadradic equation solution 2”) +
+ return(listboth) + }

factorial() Enter the coefficient of the x^2 term from your quadratic equation: 2 Enter the coefficient of the x term from your quadratic equation: -1 Enter the constant from your quadratic equation: -1 Error in -b : invalid argument to unary operator factorialf = function(num) { x <- 1 num <- as.integer(readline(prompt=“Enter a number:”)) if(num < 0) { print(“Sorry, factorial does not exist for negative numbers”) } else if(num == 0) { print(“The factorial of 0 is 1”) } else { for (i in 2:num) { x <- x * i } print(paste(“The factorial of”, num , “is”,x)) } } factorialf() Enter a number: 12 [1] “The factorial of 12 is 479001600” factorialf() Enter a number: 5 [1] “The factorial of 5 is 120” factorialf() Enter a number: 1 [1] “The factorial of 1 is 2” factorialf() Enter a number: 8 [1] “The factorial of 8 is 40320”

factorialz <- function(a, b, c) + { + quadformula1 <- (-b + sqrt( (b^2) - (4ac) )) / (2a) + quadformula2 <- (-b - sqrt( (b^2) - (4ac) )) / (2a) + listboth <- list(quadformula1, quadformula2) + names(listboth) <- c(“quadratic equation solution 1”, “quadradic equation solution 2”) + + return(listboth) + }

factorialz(2, -1, -1) $quadratic equation solution 1 [1] 1

$quadradic equation solution 2 [1] -0.5