Here is a fast recursive Fibonacci-like for
loop. How can it be more readable, and is it possible remove TArg
s?
public static class Fibonacci
{
public static BigInteger Calculate(BigInteger number)
{
var fibo = AnonRecursiveFiboFunc<BigInteger>(
func => (step, fibo1, fibo2) => step == number
? fibo2
: func(++step, fibo1 + fibo2, fibo1));
return fibo(0, 1, 0);
}
delegate Func<TArg1, TArg2, TArg3, TArg4>
Recursive<TArg1, TArg2, TArg3, TArg4>(
Recursive<TArg1, TArg2, TArg3, TArg4> r);
private static Func<TArg, TArg, TArg, TArg>
AnonRecursiveFiboFunc<TArg>(Func<Func<TArg, TArg, TArg, TArg>,
Func<TArg, TArg, TArg, TArg>> function)
{
Recursive<TArg, TArg, TArg, TArg> recursive =
rec => (step, fibo1, fibo2) =>
function(rec(rec))(step, fibo1, fibo2);
return recursive(recursive);
}
}