在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:learn-co-curriculum/fibonacci-recursive开源软件地址:https://github.com/learn-co-curriculum/fibonacci-recursive开源编程语言:Ruby 73.2%开源软件介绍:Day 3: Recursive Fibonacci SeriesLearning Goals
InstructionsFind the nth element in the Fibonacci series. The Fibonacci sequence starts with a 0 followed by a 1. After that, every value is the sum of the two values preceding it. Here are the first seven values as an example: 0, 1, 1, 2, 3, 5, 8.
If you solved this problem before iteratively, you may wish to convert that solution to a recursive version. Here are two iterative solutions - one in Ruby and one in JS: def fibonacci(n)
return n if n < 2
values = [0, 1]
(n - 1).times do
values << values[-1] + values[-2]
end
values.last
end function fibonacci(n) {
if (n < 2) {
return n;
}
const values = [0, 1];
for (let i = 0; i < n - 1; ++i) {
values.push(values[values.length - 1] + values[values.length - 2]);
}
return values[values.length - 1];
} Stuck? Here are some hints:
Use the language of your choosing. We've included starter files for some languages where you can pseudocode, explain your solution and code. Before you start coding
And remember, don't run our tests until you've passed your own! How to run your own testsRuby
JavaScript
How to run our testsRuby
JavaScript
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论