Project Euler in R Problem 2
This problem deals with one of my favorite concepts in mathematics: Fibonacci numbers.
“By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.”
To solve this problem, I first created a numeric vector and defined its first two entries to be 1 (just like the Fibonacci series). I then used the repeat function for i > 3 to solve for the ith position of the Fib vector recursively. In a Fibonacci sequence the first two terms (not counting the “0” term) are 1 and every next term is the sum of the two previous ones.
I had my repeat function break AFTER exceeding the value four million so I then had to remove the last term from my vector since it was greater than four million. Finally, I used the mod function in R to find out which positions in the Fib vector contained even terms, and summed those terms up.
1 2 3 4 5 6 7 8 9 10 11 12 13 14