Talk:Fibonacci sequence: Difference between revisions

From Rosetta Code
Content added Content deleted
(New page: The task states <tt>Solutions can be iterative or recursive (though recursive solutions are generally considered too slow and are mostly used as an exercise in recursion).</tt> while th...)
 
(New section: negative n errors)
Line 8: Line 8:


And yet other way 'round: does performance matter here on RC? Almost all code samples I've ever contributed would have to be changed if I cared about speed of execution -- I've been trying to optimize for clarity of exposition instead. Is there some kind of guideline / a practical consensus / an ongoing discussion about this somewhere?[[User:Sgeier|Sgeier]] 14:16, 7 April 2008 (MDT)
And yet other way 'round: does performance matter here on RC? Almost all code samples I've ever contributed would have to be changed if I cared about speed of execution -- I've been trying to optimize for clarity of exposition instead. Is there some kind of guideline / a practical consensus / an ongoing discussion about this somewhere?[[User:Sgeier|Sgeier]] 14:16, 7 April 2008 (MDT)

== negative n errors ==

The task description contains: "Support for negative n errors is optional."
Negative n isn't an error; while conventionally only the definition for positive n is given, the Fibonaccy sequence is actually defined for arbitrary n. You can rewrite the recursion formula F<sub>n+1</sub>=F<sub>n</sub>+F<sub>n-1</sub> as F<sub>n-1</sub>=F<sub>n+1</sub>-F<sub>n</sub> and thus extend the recursive definition to negative values. This results in
:<math>F<sub>0</sub>=0</math>
:<math>F<sub>-n</sub>=(-1)<sup>n+1</sup>F<sub>n</sub>
If the word "errors" is removed from the description, it becomes reasonable. --[[User:Ce|Ce]] 14:40, 7 April 2008 (MDT)

Revision as of 20:40, 7 April 2008

The task states

Solutions can be iterative or recursive (though recursive solutions are generally considered too slow and are mostly used as an exercise in recursion).

while the parenthetical comment is true, I'm wondering what to make of it. Is there any application of FIbonacci numbers where "computation time" is actually an issue? Do we care about computation time at all? If we do, I'd use neither an iterative nor a recursive approach but an analytic solution through phi (I added that to IDL, I see from a glance that at least the D solution has one of these as well).

I guess my question is: if we care about speed, why demand that the solution be iterative or recursive? Or, more generally, if performance is a concern, why demand any particular approach at all -- since the "best" approach for any one task is bound to be different for different languages. (Something with decent tail recursion might have recursive solutions faster than iterative ones and what-have-you).

And yet other way 'round: does performance matter here on RC? Almost all code samples I've ever contributed would have to be changed if I cared about speed of execution -- I've been trying to optimize for clarity of exposition instead. Is there some kind of guideline / a practical consensus / an ongoing discussion about this somewhere?Sgeier 14:16, 7 April 2008 (MDT)

negative n errors

The task description contains: "Support for negative n errors is optional." Negative n isn't an error; while conventionally only the definition for positive n is given, the Fibonaccy sequence is actually defined for arbitrary n. You can rewrite the recursion formula Fn+1=Fn+Fn-1 as Fn-1=Fn+1-Fn and thus extend the recursive definition to negative values. This results in

<math>F-n=(-1)n+1Fn

If the word "errors" is removed from the description, it becomes reasonable. --Ce 14:40, 7 April 2008 (MDT)