p2: Problem 2 - Even Fibonacci numbers

Description Usage Arguments Value Functions FibSeq FibStart FibNext Examples

View source: R/problem2.R

Description

This is a solution to Euler problem 2.

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

Usage

1
2
3
4
5
6
7
p2()

fibSeq(x, lim = 100, value = 1e+06)

fibStart(x)

fibNext(x)

Arguments

x

an integer number for which the following number in the Fibonacci sequence will be generated

\loadmathjax
lim

a length limit of the Fibonacci sequence to return

value

a maximum value of the Fibonacci sequence to calculate to

Value

a double vector

Functions

FibSeq

This function does not discriminate against starting integers. This means any number may be input as x and a Fibonacci-like sequence will be calculated from that starting point. The actual Fibonacci sequence will only be generated by inputting a number actually in the Fibonacci sequence.

FibStart

This function generates the first three numbers of the Fibonacci sequence as necessary (0, 1, 1). This is necessary because these numbers can not be generated by utilizing the golden ratio (see fibNext for further detail). If given a number other than 0 or 1, this function will simply return that number.

FibNext

This function generates the next number of the Fibonacci sequence given a preceding number, x. The ratio of any two consecutive entries in the Fibonacci sequence rapidly approaches \mjeqn\varphiascii. x is multipled by \mjeqn\varphiascii and rounded to the nearest integer to obtain the next number in sequence.

Note this will not give the correct next number if x = 0 or x = 1.

\mjdeqn \varphi

=\frac1+\sqrt52 ascii

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10

dylanrussellmd/projecteuler documentation built on Oct. 10, 2020, 3:34 p.m.