我认为这是可能的,但可能相当复杂。如果你这样写(见下文),它不会出错,但需要相当长的时间:
sub_Ackermann1 <- function(df){
i <- nrow(df)
m <- df$m[i]
n <- df$n[i]
if (m == 0){
r <- n+1
df$r[i] <- r
df_i <- df}
else if (m > 0 & n == 0){
r <- NA
m <- m-1
n <- 1
df_i <- df
newrow <- data.frame(m=m,n=n,r=r)
df_i <- rbind(df_i,newrow)}
else if (m > 0 & n > 0){
r1 <- NA
m1 <- m-1
n1 <- NA
df_i <- df
newrow1 <- data.frame(m=m1,n=n1,r=r1)
df_i <- rbind(df_i,newrow1)
r2 <- NA
m2 <- m
n2 <- n-1
newrow2 <- data.frame(m=m2,n=n2,r=r2)
df_i <- rbind(df_i,newrow2)}
return(df_i)
}
sub_Ackermann2 <- function(df){
r <- df$r[nrow(df)]
if (is.na(df$n[nrow(df)-1])){
df$n[nrow(df)-1] <- r }
else if (is.na(df$r[nrow(df)-1])){ df$r[nrow(df)-1] <- r}
df_i <- df[-nrow(df),]
return(df_i)
}
Ackermann <- function(m,n){
df <- data.frame(m=m,n=n,r=NA)
if (m == 0){df$r <- n+1}
while (is.na(df$r[1])){
if (is.na(df$r[nrow(df)])){ df <- sub_Ackermann1(df)}
else if (is.na(df$r[1])){ df <- sub_Ackermann2(df)}
}
return(df$r[1])
}
它至少适用于较小的值,并且不会在较大的值上崩溃。也许有人可以证明这是行不通的,反之亦然,有想法如何优化它......