Skip to content

Instantly share code, notes, and snippets.

@jmbarbone
Created October 9, 2025 04:20
Show Gist options
  • Select an option

  • Save jmbarbone/22603559d83775460932f20590789163 to your computer and use it in GitHub Desktop.

Select an option

Save jmbarbone/22603559d83775460932f20590789163 to your computer and use it in GitHub Desktop.
'fib'-o-nocci; or
.fibs <- new.env()

.fib <- function(n) {
  name <- as.character(n)
  get0(name, envir = .fibs) %||% assign(name, fibonocci(n), envir = .fibs)
}

fibonocci <- function(n = 0) {
  n <- floor(n)
  if (n <= 1L) {
    return(n)
  }
  
  .fib(n - 1L) + .fib(n - 2L)
}

# starts producing wrong answers at 79
system.time(print(format(fibonocci(500), scientific = FALSE)))
#> [1] "139423224561697698330489613862193018947914545343780323868775030943672596190605867771863846635039486902272"
#>    user  system elapsed 
#>   0.006   0.001   0.007

Created on 2025-10-09 with reprex v2.1.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment