Probability Puzzles in R
Peter Chi
Assistant Professor of Statistics<br> Villanova University
$$ x^2 + 3x + 2$$
$$ x^2 + 3x + 2 = (x+2)(x+1)$$
Quadratic Equation: $ ax^2 + bx + c = 0 $
Quadratic Formula: $ x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a} $
Discriminant: $ b^2 - 4ac $
Quadratic Formula: $ x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a} $
Discriminant: $ b^2 - 4ac $
Example. $x^2 + 3x + 2$
if(3^2 - 4*1*2 < 0){
return(FALSE)
}
Quadratic Formula: $ x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a} $
Discriminant: $ b^2 - 4ac $
Example. $x^2 + 3x + 2$
sqrt_dscr <- sqrt(3^2 - 4*1*2)
sqrt_dscr == round(sqrt_dscr)
TRUE
is.integer
does not work:
is.integer(sqrt_dscr)
FALSE
Square root is not evaluated:
value <- (-1)
if(value < 0){
print("The value is negative.")
} else {
print(sqrt(value))
}
"The value is negative."
Value is positive, so square root is evaluated:
value <- 4
if(value < 0){
print("The value is negative.")
} else {
print(sqrt(value))
}
2
for(i in 1:10){
for(j in 1:10){
print(i+j)
}
}
2
3
4
5
6
7
8
9
10
11
3
4
5
...
Probability Puzzles in R