Skip to main content
deleted 31 characters in body
Source Link
user18932
user18932

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.

Try it online!Try it online!

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);
  }
}

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.

Try it online!

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);
  }
}

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.

Try it online!

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);
  }
}
Made submission shorter, added TIO link.
Source Link
user18932
user18932

Java, 6565 61 bytes

Golfed:

n->{int a=1,b=2;for(;n>0;;n--n>0;a-=b){b=a-b;a-=b;}Systemb;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.

Try it online!

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 -n)= {b)
          b = a - b;
          a -= b;
        }
        System.out.println(a);
      } , i);
    }
  }

  private static void f(java.util.function.Consumer<Integer> function, int n) {
    function.accept(n);
  }
}

Java, 65 bytes

Golfed:

n->{int a=1,b=2;for(;n>0;--n){b=a-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; --n) {
          b = a - b;
          a -= b;
        }
        System.out.println(a);
      } , i);
    }
  }

  private static void f(java.util.function.Consumer<Integer> function, int n) {
    function.accept(n);
  }
}

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.

Try it online!

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);
  }
}
Source Link
user18932
user18932

Java, 65 bytes

Golfed:

n->{int a=1,b=2;for(;n>0;--n){b=a-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; --n) {
          b = a - b;
          a -= b;
        }
        System.out.println(a);
      } , i);
    }
  }

  private static void f(java.util.function.Consumer<Integer> function, int n) {
    function.accept(n);
  }
}