Skip to content

Instantly share code, notes, and snippets.

@Torvaney
Last active April 11, 2018 19:35
Show Gist options
  • Save Torvaney/c1bc6b2546986cf2eaf4337373817e70 to your computer and use it in GitHub Desktop.
Save Torvaney/c1bc6b2546986cf2eaf4337373817e70 to your computer and use it in GitHub Desktop.
Stirling formula ~ Factorial
library(tidyverse)
stirling_formula <- function(n) sqrt(2*pi*n)*(n / exp(1))^n
x <- 1:10
breaks_log10 <- 1:10 %>%
map_dbl(~ 10^.x) %>%
map(~ .x*(1:10)) %>%
unlist()
tibble(
x,
factorial = factorial(x),
stirling = stirling_formula(x)
) %>%
ggplot(aes(x = factorial, y = stirling)) +
geom_abline(slope = 1, intercept = 0, linetype = "dotted") +
geom_label(aes(label = x)) +
scale_x_log10(minor_breaks = breaks_log10) +
scale_y_log10(minor_breaks = breaks_log10) +
xlab("n!") + ylab("Stirling(n)") +
ggtitle("Stirling formula ~ Factorial",
"√(2πn).(n/e)^e for n = 1:10")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment