Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.
Posted 01 July 2012 - 09:03 PM
Posted 01 July 2012 - 10:11 PM
Posted 02 July 2012 - 12:17 AM
int fib(int n) { if (n < 2) return n; else return fib(n-1) + fib(n-2); }
openwar - the real-time tactical war-game platform
Posted 02 July 2012 - 12:31 AM
Posted 03 July 2012 - 02:55 AM
Posted 03 July 2012 - 03:27 AM
It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.
Posted 03 July 2012 - 03:32 AM
#include <iostream> // M represents a matrix of the form // (a b ) // (b a+b) struct M { long a, b; M(long a, long b) : a(a), b(b) { } }; M operator*(M x, M y) { return M(x.a*y.a + x.b*y.b, x.a*y.b + x.b*y.a + x.b*y.b); } // Fast exponentiation M pow(M x, unsigned n) { if (n==0) return M(1,0); if (n==1) return x; if (n%2==0) return pow(x*x,n/2); if (n%3==0) return pow(x*x*x,n/3); return x*pow(x,n-1); } // Compute // (0 1)^n = (fib(n-1) fib(n) ) // (1 1) ( fib(n) fib(n+1)) long fib(unsigned n) { M m = pow(M(0,1),n); return m.b; } int main() { std::cout << fib(40) << '\n'; }
Edited by alvaro, 03 July 2012 - 11:21 AM.
Posted 03 July 2012 - 06:44 AM
By that logic, there's "no use for recursion" in tail recursive functions at all and hence no point to the idea of tail recursion, because an existing tail recursive function can be trivially transformed into an iterative form as well. Just update the parameter variables and jump to the beginning of the function body, and you have your iterative version. However, not all languages are the same in terms of control structures, and iteration is non-idiomatic in many, if not most, functional languages. In those languages a tail recursive transformation has the same process and the same point as a manual transformation to an iterative version in an imperative language. In other words, a completely pointless waste of programmer time and computing resources in the ordinary case, but useful once in a while. Ultimately, they all express same algorithm, the difference is in the control structures used to implement that algorithm: regular recursion, tail recursion or iteration. Whether or not the change of expression of an algorithm represents a different "function" is a semantic argument.Anything can be rewritten an infinite number of ways, but in the purest sense, no not every recursive function can be rewritten tail-recursive instead. If it could then via a straightforward transformation we could change those functions to use iteration (which is generally more efficient) and have no use for recursion any more.
Using an explicit stack and tacking on some unrelated meaningless tail-recursion does not count as converting the algorithm to a tail-recursive implementation.
Incorrect. Fibonacci can be written tail recursively. There's just no point to writing the naive implementation tail recursively because it requires explicitly duplicating the stack structure that the recursive calls automatically generate, and this implicit stack is almost always faster than any explicit stack structure. Any recursive algorithm can be written to use an explicit stack in place of an implicit stack of function calls. This includes Fibonacci, tree-traversal, searches, sorts or whatever recursive algorithm you can imagine. You push data on an explicitly managed stack that would normally be passed as parameters in a recursive call and then pop the stack as computations progress. Again, this is the same process that you would use to transform a recursive function into an iterative version of the same function. The only difference that instead of a loop operating on the explicit stack you use a tail recursive call on the explicit stack.Fibonacci cannot be written tail recursively as said, Some sorting algorithms that are written recursively I believe can't be tail recursive either.
(define (fib n) (define (fib-aux a b p q count) (cond ((= count 0) b) ((even? count) (fib-aux a b (+ (* p p) (* q q)) (+ (* q q) (* 2 p q)) (/ count 2))) (else (fib-aux (+ (* b q) (* a q) (* a p)) (+ (* b p) (* a q)) p q (- count 1))))) (fib-aux 1 0 0 1 n))
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.
GameDev.net™, the GameDev.net logo, and GDNet™ are trademarks of GameDev.net, LLC.