Skip to content

Instantly share code, notes, and snippets.

@kiljacken
Created November 5, 2015 13:06
Show Gist options
  • Save kiljacken/466b1705d3206c999ae6 to your computer and use it in GitHub Desktop.
Save kiljacken/466b1705d3206c999ae6 to your computer and use it in GitHub Desktop.
Fibonacci in java using a horrible state machine.
public class Fibo
{
public static int fib(int n) {
State[] sss = new State[]{
(l) -> new int[]{ l[0], l[1], l[2], l[3], 0},
(l) -> new int[]{ l[0], l[1], l[3]>0?l[3]!=0?l[3]!=1?6:5:4:3, l[3], l[4]},
(l) -> new int[]{ l[0], l[1], l[3]>0?6:0, l[3], l[4]},
(l) -> new int[]{ -1, l[1], 0, l[3], l[4]},
(l) -> new int[]{ 0, l[1], 0, l[3], l[4]},
(l) -> new int[]{ 1, l[1], 0, l[3], l[4]},
(l) -> new int[]{l[0]+l[1], l[0], 2, l[3]-1, l[4]},
};
int[] l = new int[]{0,1,1,n,1};
while(l[4]>0) { l = sss[l[2]].a(l); }
return l[0];
}
private static interface State {
public abstract int[] a(int[] l);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment