aspose file tools*
The moose likes Beginning Java and the fly likes Fibonacci Recursive Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Fibonacci Recursive" Watch "Fibonacci Recursive" New topic
Author

Fibonacci Recursive

Stephen Foy
Ranch Hand

Joined: Oct 17, 2005
Posts: 143
I wrote this basic fibonacci recursive method, as a way of learning recursion. But compared to an older version of the same program this one runs much slower. Is this common with recursion? what could be causing this?


[ April 02, 2006: Message edited by: Stephen Foy ]

Stephen Foy - Microsoft Application Development Consultant
Stephan Norwood
Greenhorn

Joined: Mar 23, 2006
Posts: 14
Hi Stephen,

calculating the 100th Fibonacci number is very time consuming.(as you know I presume)

recursive method explained:
The process builds up a chain of deferred operations.
(in this case,a chain of additions-->recursive process).
If you want to visualize this,take a paper and a pencil and substitute the appropriate values.

Carrying out this process requires that the interpreter keep track of the operations to be performed.The length of the chain (and the information needed to be kept ) grows linearly with n.
(Big O notation O(n) )

Hope this helps

[ April 02, 2006: Message edited by: Stephan Norwood ]
[ April 02, 2006: Message edited by: Stephan Norwood ]
Stephen Foy
Ranch Hand

Joined: Oct 17, 2005
Posts: 143
Thanks, that clears it up perfectly
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41489
    
  53
Just nitpicking here, but since each call to "calculate" triggers two more calls, this isn't linear complexity -O(n)-, but exponential complexity - O(2^n). That's what makes Fibonacci numbers such a bad example of recursion: It can be calculated with linear complexity, but calculated recursively, it's exponential.

Calculating factorials is an example of a linear recursion.
[ April 02, 2006: Message edited by: Ulf Dittmer ]

Ping & DNS - my free Android networking tools app
Stephan Norwood
Greenhorn

Joined: Mar 23, 2006
Posts: 14
Yep,a mistake on my part.Recursive calculation is exponential.(in this case )
[ April 03, 2006: Message edited by: Stephan Norwood ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Fibonacci Recursive