Other sites

Project Euler — problem 25

Finally, the fog lasting for days went away this morning and the sun comes out. It’s a lovely winter day. After taking a walk after lunch, I feel like doing some math. So, here comes the 25th Euler problem.

The Fibonacci sequence is defined by the recurrence relation: Fn = Fn1 + Fn2, where F1 = 1 and F2 = 1. What is the first term in the Fibonacci sequence to contain 1000 digits?

Well, it’s a simple Fibonacci problem, which I have solved several times before. The only issue is that I have to deal with big integers. For the reason of convenience, I’m gonna use the helpful “gmp” package, again. Hope nobody minds :p

library(gmp)
first <- as.bigz(1)
second <- as.bigz(1)
third <- first + second
n <-2while(nchar(as.character(third))<1000){
n <- n +1
third <- first + second
first <- second
second <- third
}cat("The result is:", n, "\n")