如何在 R 中完成此斐波那契数列评估?
How can I complete this Fibonacci sequence evaluation in R?
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
我需要的是关于我应该从这里走向何方的一些指导。
我应该创建一个 data.table 并使用 SQL 包查询它,还是有更优雅、更简单的方法?
提前致谢!
要从前 50 个斐波那契数中找出偶数,您可以使用这个
even_numbers <- fibvals[fibvals%%2==0]
然后通过计算这些偶数的累加和,加上和的最大值条件,你可以select这些偶数
cumsum(even_numbers)<500000
因此您想要的斐波那契数是
even_numbers[cumsum(even_numbers)<500000]
他们的sum
是
sum(even_numbers[cumsum(even_numbers)<500000])
这样就可以了
fsum <- 0
for (i in 1:len) { if (is.even(fibvals[i]) && (fsum + fibvals[i])<=500000) {fsum = fsum + fibvals[i]}}
总和将存储在 fsum
。
这里有一个使用递归函数的方法:
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()
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
我需要的是关于我应该从这里走向何方的一些指导。 我应该创建一个 data.table 并使用 SQL 包查询它,还是有更优雅、更简单的方法?
提前致谢!
要从前 50 个斐波那契数中找出偶数,您可以使用这个
even_numbers <- fibvals[fibvals%%2==0]
然后通过计算这些偶数的累加和,加上和的最大值条件,你可以select这些偶数
cumsum(even_numbers)<500000
因此您想要的斐波那契数是
even_numbers[cumsum(even_numbers)<500000]
他们的sum
是
sum(even_numbers[cumsum(even_numbers)<500000])
这样就可以了
fsum <- 0
for (i in 1:len) { if (is.even(fibvals[i]) && (fsum + fibvals[i])<=500000) {fsum = fsum + fibvals[i]}}
总和将存储在 fsum
。
这里有一个使用递归函数的方法:
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()