Csalculzate第n个斐波那契数n<> 1000 [英] Csalculzate the nth fibonacci number n<> 1000

查看:90
本文介绍了Csalculzate第n个斐波那契数n<> 1000的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编写一种方法来计算n <1000的第n个Fibonacci数。第n个斐波纳契数是前两个数的总和。前两个数字都是1.对于n <= 0或n> 1000,返回0。例如,

斐波那契(1)返回1

斐波那契(2)返回1

斐波那契(3)返回2

斐波那契(4)返回3

斐波那契(5)返回5

斐波那契(6)返回8

斐波那契(N)返回斐波那契(N-1)+斐波纳契(N-2)

首先完成下面的简单方法。然后描述如何提高效率。

public int Fibonacci(int N)

{

if(N< = 0 | | N> 1000)



我尝试过:



我不知道如何做这个写一个方法来计算n <1000的第n个Fibonacci数。第n个斐波纳契数是前两个数的总和。前两个数字都是1.对于n <= 0或n> 1000,返回0。例如,

斐波那契(1)返回1

斐波那契(2)返回1

斐波那契(3)返回2

斐波那契(4)返回3

斐波那契(5)返回5

斐波那契(6)返回8

斐波那契(N)返回斐波那契(N-1)+斐波纳契(N-2)

首先完成下面的简单方法。然后描述如何提高效率。

public int Fibonacci(int N)

{

if(N< = 0 | | N> 1000)

解决方案

我们不打算为你做功课。



如果您对卡住的地方有疑问,那么请详细说明问题。


一位聪明人曾经说过



理解递归你必须首先理解递归



如上所述,至少有三种方法可以解决这个问题 - 迭代,或递归,或矩阵(唐纳德)在O(log(n))时间内的Knuth算法!



对于一个小'n',它可能无论是堆栈方式,迭代方式还是递归方式都无关紧要。



所以,为什么不迭代地或递归地给出一个镜头,当你遇到特定的问题时回复,如果/当你遇到困难时


< blockquote>问题很简单。它的定义实际上是对算法的描述:斐波纳契数 - 维基百科,免费的百科全书



显然算法中没有任何递归。相反,它是经常性,这是不一样的:

递归关系 - 维基百科,免费的百科全书



解决方案应该是纯粹的迭代,只需一个简单的循环。请注意,您甚至不需要当前序列。你只需要知道前面两个元素,你必须存储在循环外面。



-SA

Write a method to calculate the nth Fibonacci number for n<1000. The nth Fibonacci number is the sum of the two previous numbers. The first two numbers are both 1. Return 0 for n<=0 or n>1000. E.g.,
Fibonacci(1) returns 1
Fibonacci(2) returns 1
Fibonacci(3) returns 2
Fibonacci(4) returns 3
Fibonacci(5) returns 5
Fibonacci(6) returns 8
Fibonacci(N) returns Fibonacci(N-1) + Fibonacci(N-2)
First complete the simple method below. Then describe how you could make it more efficient.
public int Fibonacci(int N)
{
if (N <= 0 || N > 1000)

What I have tried:

I have no clue how to do this Write a method to calculate the nth Fibonacci number for n<1000. The nth Fibonacci number is the sum of the two previous numbers. The first two numbers are both 1. Return 0 for n<=0 or n>1000. E.g.,
Fibonacci(1) returns 1
Fibonacci(2) returns 1
Fibonacci(3) returns 2
Fibonacci(4) returns 3
Fibonacci(5) returns 5
Fibonacci(6) returns 8
Fibonacci(N) returns Fibonacci(N-1) + Fibonacci(N-2)
First complete the simple method below. Then describe how you could make it more efficient.
public int Fibonacci(int N)
{
if (N <= 0 || N > 1000)

解决方案

We're not going to do your homework for you.

If you have a question about where you're stuck, great, describe the problem.


A wise man once said

"to understand recursion you must first understand recursion"

that being said, there's at least three ways to solve this issue - iteratively, or recursively, or matrix (Donald Knuth algorithm) in O(log(n)) time !

for a small 'n', it probably doesnt matter stack-wise, iteratively or recursively would do.

So, why dont you give iteratively or recursively a shot, posting back when you have specific questions if/when you get stuck


The problem is quite simple. It definition is, practically, the description of the algorithm: Fibonacci number — Wikipedia, the free encyclopedia.

It's apparent that there is nothing recursive in the algorithm. Instead, it is recurrent, which is not the same:
Recurrence relation — Wikipedia, the free encyclopedia.

The solution should be purely iterative, in just one simple loop. Note that you don't even need to current sequence. You only need to know two previous elements, which you have to store outside the loop.

—SA


这篇关于Csalculzate第n个斐波那契数n&lt;&gt; 1000的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆