R 中的概率谜题
Peter Chi
Assistant Professor of Statistics<br> Villanova University
$$ x^2 + 3x + 2$$
$$ x^2 + 3x + 2 = (x+2)(x+1)$$
二次方程: $ ax^2 + bx + c = 0 $
求根公式: $ x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a} $
判别式: $ b^2 - 4ac $
求根公式: $ x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a} $
判别式: $ b^2 - 4ac $
示例: $x^2 + 3x + 2$
if(3^2 - 4*1*2 < 0){
return(FALSE)
}
求根公式: $ x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a} $
判别式: $ b^2 - 4ac $
示例: $x^2 + 3x + 2$
sqrt_dscr <- sqrt(3^2 - 4*1*2)
sqrt_dscr == round(sqrt_dscr)
TRUE
is.integer 不适用:
is.integer(sqrt_dscr)
FALSE
未计算平方根:
value <- (-1)
if(value < 0){
print("The value is negative.")
} else {
print(sqrt(value))
}
"The value is negative."
值为正,因此计算平方根:
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
...
R 中的概率谜题