All pastes #481413 Raw Edit

Miscellany

public text v1 · immutable
·published 2007-05-10 20:23 UTC
rendered paste body
import java.math.BigInteger;

class Fibonacci {
	
	public static void fibonacci (BigInteger i) {
		BigInteger n0 = BigInteger.ZERO;
		BigInteger n1 = BigInteger.ONE;
		BigInteger n  = BigInteger.ONE;
		BigInteger nn = BigInteger.ZERO;

		long t0 = System.currentTimeMillis();
		long t1 = t0;
		long t2;

		while (i.compareTo(n) > 0) {
			nn = n0.add(n1);

			t2 = System.currentTimeMillis();
			if ((t2 - t1) > 5000) {
				System.out.println(n.toString() + " (" + nn.bitLength() + " bit) -- " + 
						Runtime.getRuntime().freeMemory() / 1024 + "k of " + 
						Runtime.getRuntime().totalMemory() / 1024 + "k");
				
				t1 = System.currentTimeMillis();
			}
			
			n0 = n1;
			n1 = nn;			
			n = n.add(BigInteger.ONE);
		}

		System.out.println(n.toString() + " (" + nn.bitLength() + " bit)");
		System.out.println((System.currentTimeMillis() - t0) / 1000 + " seconds");
	}
	
	public static void main (String... args) {
		fibonacci(new BigInteger(args[0]));
	}
}