Skip to content

Instantly share code, notes, and snippets.

@djeikyb
Created May 30, 2013 20:08
Show Gist options
  • Select an option

  • Save djeikyb/5680749 to your computer and use it in GitHub Desktop.

Select an option

Save djeikyb/5680749 to your computer and use it in GitHub Desktop.
playing with fibonacci sequence generators
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Fibonacci
{
public static void main(String...args)
{
// fib(new BigInteger("7886"));
fib(273000);
// fib(new BigInteger("10"));
// Fibocrappy.main(new String[]{"43"});
}
private static void fib(int steps)
{
long start = 0;
long end = 0;
List<BigInteger> fibbo = null;
// // WARM UP
// for (int i = 0; i < 1000; i += 1)
// {
// fibbo = fibberiter(steps);
// }
// // end WARMING
// for (int i = 0; i < 1000; i += 1)
// {
start = System.nanoTime();
fibbo = fibberiter(steps);
end = System.nanoTime();
// System.out.println(fibbo);
// System.out.println(fibbo.get(fibbo.size()-1));
// }
System.out.println("time took: " + ((end - start)/1000000.0F) + "ms");
// for (int i = 0; i < 1000; i += 1)
// {
// start = System.nanoTime();
// fibbo = fibbertail(BigInteger.ZERO,BigInteger.ONE,steps,null);
// end = System.nanoTime();
//// System.out.println(fibbo);
//// System.out.println(fibbo.get(fibbo.size()-1));
// }
// System.out.println("time took: " + ((end - start)/1000000.0F) + "ms");
// for (int i = 0; i < 1000; i += 1)
// {
// start = System.nanoTime();
// fibbo = fibber(BigInteger.ZERO,BigInteger.ONE,steps);
// end = System.nanoTime();
//// System.out.println(fibbo);
//// System.out.println(fibbo.get(fibbo.size()-1));
// }
// System.out.println("time took: " + ((end - start)/1000000.0F) + "ms");
}
private static List<BigInteger> fibberiter(int steps)
{
List<BigInteger> result = new ArrayList<BigInteger>(steps);
BigInteger a = BigInteger.ZERO;
BigInteger b = BigInteger.ONE;
for (int i = 0; i < steps; i += 1)
{
try
{
result.add(a);
BigInteger ab = null;
ab = a.add(b);
a = b;
b = ab;
}
catch (OutOfMemoryError e)
{
System.out.println(i);
// throw e;
e.printStackTrace();
return null;
}
}
return result;
}
private static List<BigInteger> fibber(BigInteger a, BigInteger b, BigInteger step)
{
List<BigInteger> result = new ArrayList<BigInteger>();
if (step.compareTo(BigInteger.ZERO) > 0)
{
step = step.subtract(BigInteger.ONE);
result.add(a);
result.addAll(fibber(b, a.add(b), step));
}
return result;
}
private static List<BigInteger> fibbertail(BigInteger a, BigInteger b, int steps, List<BigInteger> list)
{
if (list == null) list = new ArrayList<BigInteger>(steps);
list.add(a);
try
{
return (steps == 0) ? Collections.<BigInteger>emptyList() : (list.size() >= steps) ? list : fibbertail(b, a.add(b), steps, list);
}
catch (StackOverflowError e)
{
System.out.println(list.size());
// throw e;
e.printStackTrace();
return null;
}
}
/**
* stole online as reference for crappy implementation
*/
public static class Fibocrappy {
public static long fib(int n) {
if (n <= 1) return n;
else return fib(n-1) + fib(n-2);
}
public static void main(String[] args) {
int N = Integer.parseInt(args[0]);
for (int i = 1; i <= N; i++)
System.out.println(i + ": " + fib(i));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment