未验证 提交 03664dec 编写于 作者: V vonzo 提交者: GitHub

Fibonacci funtion added (#767)

* finbonacci funtion added

* finbonacci funtion added

* finbonacci funtion added

* finbonacci funtion added

* Update fibonacci.cpp
Co-authored-by: NChristian Clauss <cclauss@me.com>
上级 a11c09e6
#include <iostream>
#include <cassert>
/* Calculate the the value on Fibonacci's sequence given an
integer as input
Fibonacci = 0, 1, 1, 2, 3, 5,
8, 13, 21, 34, 55,
89, 144, ... */
int fibonacci(uint n) {
/* If the input is 0 or 1 just return the same
This will set the first 2 values of the sequence */
if (n <= 1)
return n;
/* Add the last 2 values of the sequence to get next */
return fibonacci(n-1) + fibonacci(n-2);
}
int main() {
int n;
std::cin >> n;
assert(n >= 0);
std::cout << "F(" << n << ")= " << fibonacci(n) << std::endl;
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册