Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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)
Posted
Updated 17-Jun-16 21:50pm

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

If you have a question about where you're stuck, great, describe the problem.
 
Share this answer
 
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
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-Jun-16 3:58am    
Sorry... (sigh)... I think if you were a little slower in your effort to produce an answer, you would easily see that there is absolutely no any reasonable room for recursive algorithm. Usually, the benefit of recursion is the ease of the implementation when the formulation of the problem is recursive; then one would just act by definition. Obviously, you understand that even in this cases iterative solution can be possible and better, but not always.

However, Fibonacci formulation has is not really recursive. Of course, it cannot be formulated in a recursive way, but only artificially. The original and simplest formulation of the sequence is recurrent, which is not the same as recursive. Look at the formulation, and you will see that it directly suggests the simple iteration solution.

Please see Solution 3.

—SA
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900