Compensating for my cores being 2.5GHz by allowing just under 8 seconds,
public class FiboSum
{
public static void main(String[] args)
{
long now = System.currentTimeMillis();
System.out.println("Taking F(1) = F(2) = 1, F(n) = F(n-1) + F(n-2):");
long n = 0, x = 1, y = 1, N = 1000000000;
// x = F_n, y = F_{n+1}
while (System.currentTimeMillis() - now < 7800)
{
n++;
long t = (x*x + y*y) % N;
x = x * (2*y - x) % N;
y = t;
}
System.out.println("\\sum_{r=1}^{2^"+n+"} F(r) = " + (N+x+y-1)%N + " mod " + N);
}
}
Output:
Taking F(1) = F(2) = 1, F(n) = F(n-1) + F(n-2):
\sum_{r=1}^{2^94900480} F(r) = 755986263 mod 1000000000
And no sign of Binet's formula or any closed form evaluation of the sum, even though there is a trivial one.
As a closely related point of interest, note that we can use the identity F(m+n) = F(m+1) F(n) + F(m) F(n+1) - F(m) F(n) to compute the nth Fibonacci number in O(lg n) arithmetic operations. Replace int and long with a suitable BigInteger implementation to taste. I've made this tail-recursive.
static long Fibo(int n)
{
if (n > 92) throw new ArithmeticException("Overflow");
long Fm = 1, Fmp = 1;
long Fn = 0, Fnp = 1;
while (n > 0)
{
if ((n & 1) == 1)
{
long t = Fmp * Fn + Fm * (Fnp - Fn);
Fnp = Fmp * Fnp + Fm * Fn;
Fn = t;
}
long t = Fm * (2*Fmp - Fm);
Fmp = Fmp*Fmp + Fm*Fm;
Fm = t;
n >>= 1;
}
return Fn;
}
Or if SML is your cup of tea then
fun fibo x =
let fun f 0 _ _ Fn _ = Fn
| f n Fm Fmp Fn Fnp =
let val Fm2 = Fm * (2 * Fmp - Fm)
val Fmp2 = Fmp * Fmp + Fm * Fm
in if (n mod 2 = 0)
then f (n div 2) Fm2 Fmp2 Fn Fnp
else f (n div 2) Fm2 Fmp2 (Fmp * Fn + Fm * (Fnp - Fn)) (Fmp * Fnp + Fm * Fn)
end
in f x 1 1 0 1
end;
Detailed reasoning at http://www.cheddarmonk.org/Fibonacci.html