Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,6 @@
fib=function(n,x=c(0,1)) {
if (abs(n)>1) for (i in seq(abs(n)-1)) x=c(x[2],sum(x))
if (n<0) return(x[2]*(-1)^(abs(n)-1)) else if (n) return(x[2]) else return(0)
}
sapply(seq(-31,31),fib)

View file

@ -0,0 +1,39 @@
# recursive
recfibo <- function(n) {
if ( n < 2 ) n
else Recall(n-1) + Recall(n-2)
}
# print the first 21 elements
print.table(lapply(0:20, recfibo))
# iterative
iterfibo <- function(n) {
if ( n < 2 )
n
else {
f <- c(0, 1)
for (i in 2:n) {
t <- f[2]
f[2] <- sum(f)
f[1] <- t
}
f[2]
}
}
print.table(lapply(0:20, iterfibo))
# iterative but looping replaced by map-reduce'ing
funcfibo <- function(n) {
if (n < 2)
n
else {
generator <- function(f, ...) {
c(f[2], sum(f))
}
Reduce(generator, 2:n, c(0,1))[2]
}
}
print.table(lapply(0:20, funcfibo))