如何在R中完成此Fibonacci序列评估?

时间:2016-07-02 16:26:46

标签: r math fibonacci

问候Stackland的好人!

最近我接受了这项任务

  • 以任何语言生成斐波纳契数列
  • 评估每个值是奇数还是
  • 对偶数进行求和,使得它们的总数不是> 500,000

我选择做这个R,因为我正在学习这门语言,并认为这样做是一个很好的练习。

我已设法完成任务的第2步但未能继续进行。请参阅下面的代码和评论。

   len <- 50
    fibvals <- numeric(len)
    fibvals[1] <- 1
    fibvals[2] <- 1
    for(i in 3:len) { fibvals[i] <- fibvals[i-1]+fibvals[i-2]}
    fibvals
     [1]           1           1           2           3           5
     [6]           8          13          21          34          55
    [11]          89         144         233         377         610
    [16]         987        1597        2584        4181        6765
    [21]       10946       17711       28657       46368       75025
    [26]      121393      196418      317811      514229      832040
    [31]     1346269     2178309     3524578     5702887     9227465
    [36]    14930352    24157817    39088169    63245986   102334155
    [41]   165580141   267914296   433494437   701408733  1134903170
    [46]  1836311903  2971215073  4807526976  7778742049 12586269025

    # Creates a variable called len in which the value 50 is stored
    # Creates a var called fibvals, which is a numeric datatype, which should have len (50) vals
    # Sets the value of the first entry in fibvals to 1
    # Sets the value of the second entry in fibvals to 1
    # Loop - "for (i in 3:len)" dictates that the loop should be executed between step 3 and step 50 (denoted by "len")
    # Loop - Defines a loop step "i" as being the result of the (current i - the before it) + (current i - i two before it)
    # Loop - Example 5 = (5-3) + (5-2) OR 2 + 3 = 5 | Example 21 = (21-13) + (21-8) OR  8 + 13 = 21 

    is.even <- function(x){ x %% 2 == 0 }

    # Creates a UDF to check if values are odd or even by using modulo. 
    If the remainder is 0 when any value is divided by 2, it is an even number 

is.even(fibvals)

 [1] FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE
[11] FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE
[21]  TRUE FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE  TRUE
[31] FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE
[41] FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE

# Evaluates all Fibonacci values on odd or even property

我需要的是关于我应该从哪里开始的一些指导。 我应该使用SQL包创建data.table和查询,还是有更优雅,更简单的方法?

提前致谢!

3 个答案:

答案 0 :(得分:3)

为了从最初的50个斐波那契数字中挑选出偶数,你可以使用这个

even_numbers <- fibvals[fibvals%%2==0]

然后通过计算那些偶数的累积和并强加和的最大值的条件,你可以通过这个选择那些偶数

cumsum(even_numbers)<500000

因此,您想要的斐波纳契数是

even_numbers[cumsum(even_numbers)<500000]

他们的sum

sum(even_numbers[cumsum(even_numbers)<500000])

答案 1 :(得分:0)

这样做

fsum <- 0
for (i in 1:len) { if (is.even(fibvals[i]) && (fsum + fibvals[i])<=500000) {fsum = fsum + fibvals[i]}}

总和将存储在fsum

答案 2 :(得分:0)

这是使用递归函数执行此操作的方法:

getEvenWithFibber <- function(y = c(1,1), 
                          s = 0,
                          threshold = 500000) {
 if(s + y[1] + y[2] < threshold) 
    getEvenWithFibber(y = c(y[1] + y[2],y), s =  s + ifelse(y[1]%%2==0,y[1],0)) 
 else list(sum = s, seq = y, iseven = y%%2 == 0)
}


getEvenWithFibber()