fac <- function(x) {
stopifnot(is.numeric(x), x >= 0, length(x) == 1)
if (x == 0) return(1)
if (x == 1) return(1)
return(x*fac(x-1))
}
fac(5)[1] 120
[1] 8.320987e+81
Error in `fac()`:
! x >= 0 is not TRUE
2026-01-20
the prototypical function - similar to proof by induction:
define values for endings
get value for f(x) from value of f(x-1)
Calling functions generally leads to cleaner code (function naming is crucial!)
Focus on “what” is done rather than “how”
… at the cost of memory (function stack)
Use the factorial function to implement the binomial coefficient as a function binom(n, k)
\(\binom{n}{k} = \frac{n!}{k! (n-k)!}\)
What is the result for binom(175,2)?
Rewrite the function to be able to deal with binom(175,2) and binom(175,173)
For base \(\beta\) and precision \(p\) (both \(\beta\) and \(p\) are positive integers), FPNs are represented as
\[ d.dddddd \times \beta^e, \] where \(d.dddddd\) is called the significand and has \(p\) digits.
If the first \(d\) is non-zero, the representation is called normalized. Normalized FP representations are unique.
Values outside of these limits are encoded as Inf (and -Inf for negative values) in R.