I am programming a simple recursive algorithm to calculate the Fibonacci sequence in R, just as I do in C. It follows the same thing below:
fibonacci <- function (n)
{
if (n == 1L)
return (0L)
else if (n == 2L || n == 3L)
return (1L)
else
return (fibonacci(n - 1L) + fibonacci(n - 2L))
}
n <- readline(prompt="Insira um valor inteiro: ")
n <- as.integer(n)
print(fibonacci(n))
The script seems to run smoothly in the RStudio environment. But when running on the linux console, I get the following error:
Enter an integer value: Error in if (n == 1L) return (0L) else if (n == 2L || n == 3L) return (1L) else return (fibonacci (n - missing value where TRUE / FALSE required Calls: print - > fibonacci Execution stopped
Could anyone help me?