Java, 65 61 bytes
Golfed:
n->{int a=1,b=2;for(;n-->0;a-=b)b=a-b;System.out.println(a);}
This uses the standard trick of calculating Fibonacci using only two variables to hold the state, but modified to work with subtraction. I am aware that theoretically, System.out.print()
should work here but in practice it is inconsistent with flushing output and sometimes (often enough to be an issue) will literally print nothing using the Oracle JVM.
This function is zero-indexed.
Ungolfed:
public class FibtractionFibonacciButWithSubtraction {
public static void main(String[] args) {
for (int i : new int[] { 1, 2, 11, 14, 21, 24, 31 }) {
f(n -> {
int a = 1, b = 2;
for (; n-- > 0; a -= b)
b = a - b;
System.out.println(a);
} , i);
}
}
private static void f(java.util.function.Consumer<Integer> function, int n) {
function.accept(n);
}
}