On Tue, 24 Jul 2012 19:02:07 +0200, Russel Winder <rus...@winder.org.uk> wrote:

On Tue, 2012-07-24 at 16:59 +0200, Simen Kjaeraas wrote:
[…]
...which inspired me to write this implementation of fibonacci:

T fib(T = int)(int n, T a = 0, T b = 1) {
     while ( n-- ) {
         TypeTuple!(a,b) = tuple(b, a +b);
     }
     return a;
}

Or possibly better:

long fibonacci ( immutable long n ) {
return array ( takeExactly ( recurrence ! ( "a[n-1] + a[n-2]" ) ( 0L , 1L ) , cast ( size_t ) ( n + 1 ) ) ) [ n ] ;
}

?

I had to fix things a bit (I have reasons to want the template parameter there):

T fib(T = int)(int n, T a = to!T(0), T b = to!T(1)) {
    while ( n-- ) {
        TypeTuple!(a,b) = tuple(b, a +b);
    }
    return a;
}

Now, try asking each of these for fib!BigInt(1_000_000). :p

--
Simen

Reply via email to