Skip to main content
Commonmark migration
Source Link

#Java 7, 49 48 bytes

Java 7, 49 48 bytes

int c(int a,int b){return-~a!=b?a*c(a+1,b):a*b;}

corsiKa's answer is exclusive, mine is inclusive (and slightly shorter by using recursion instead of a loop).

Explanation:

Try it here.

int c(int a,int b){  // Method with two integer parameters and integer return-type
  return-~a!=b?      //  If `a+1` is not equal to `b`
         a*c(a+1,b)  //   Return `a` multiplied by the recursive call with `a+1,b`
        :            //  Else:
         a*b;        //   Return `a*b`
}                    // End of method

#Java 7, 49 48 bytes

int c(int a,int b){return-~a!=b?a*c(a+1,b):a*b;}

corsiKa's answer is exclusive, mine is inclusive (and slightly shorter by using recursion instead of a loop).

Explanation:

Try it here.

int c(int a,int b){  // Method with two integer parameters and integer return-type
  return-~a!=b?      //  If `a+1` is not equal to `b`
         a*c(a+1,b)  //   Return `a` multiplied by the recursive call with `a+1,b`
        :            //  Else:
         a*b;        //   Return `a*b`
}                    // End of method

Java 7, 49 48 bytes

int c(int a,int b){return-~a!=b?a*c(a+1,b):a*b;}

corsiKa's answer is exclusive, mine is inclusive (and slightly shorter by using recursion instead of a loop).

Explanation:

Try it here.

int c(int a,int b){  // Method with two integer parameters and integer return-type
  return-~a!=b?      //  If `a+1` is not equal to `b`
         a*c(a+1,b)  //   Return `a` multiplied by the recursive call with `a+1,b`
        :            //  Else:
         a*b;        //   Return `a*b`
}                    // End of method
-1 byte changing `return a+1` to `return-~a`
Source Link
Kevin Cruijssen
  • 131.4k
  • 13
  • 144
  • 384

#Java 7, 4949 48 bytes

int c(int a,int b){return-~a!=b?a*c(a+1,b):a*b;}

corsiKa's answer is exclusive, mine is inclusive (and slightly shorter by using recursion instead of a loop).

int q(int a,int b){return a+1!=b?a*q(a+1,b):a*b;}

Ungolfed & test codeExplanation:

Try it here.

class Main{
    public static void main(String[] a){
        System.out.println(q(2, 5));
        System.out.println(q(5, 10));
        System.out.println(q(-4, 3));
        System.out.println(q(0, 3));
        System.out.println(q(-4, -1));
    }

    static int q(int a, int b){
        return a+1 != b
            ? a * q(a+1, b)
            : a * b;
    }
}

Output:Try it here.

120
151200
0
0
24
int c(int a,int b){  // Method with two integer parameters and integer return-type
  return-~a!=b?      //  If `a+1` is not equal to `b`
         a*c(a+1,b)  //   Return `a` multiplied by the recursive call with `a+1,b`
        :            //  Else:
         a*b;        //   Return `a*b`
}                    // End of method

#Java 7, 49 bytes

corsiKa's answer is exclusive, mine is inclusive (and slightly shorter by using recursion instead of a loop).

int q(int a,int b){return a+1!=b?a*q(a+1,b):a*b;}

Ungolfed & test code:

Try it here.

class Main{
    public static void main(String[] a){
        System.out.println(q(2, 5));
        System.out.println(q(5, 10));
        System.out.println(q(-4, 3));
        System.out.println(q(0, 3));
        System.out.println(q(-4, -1));
    }

    static int q(int a, int b){
        return a+1 != b
            ? a * q(a+1, b)
            : a * b;
    }
}

Output:

120
151200
0
0
24

#Java 7, 49 48 bytes

int c(int a,int b){return-~a!=b?a*c(a+1,b):a*b;}

corsiKa's answer is exclusive, mine is inclusive (and slightly shorter by using recursion instead of a loop).

Explanation:

Try it here.

int c(int a,int b){  // Method with two integer parameters and integer return-type
  return-~a!=b?      //  If `a+1` is not equal to `b`
         a*c(a+1,b)  //   Return `a` multiplied by the recursive call with `a+1,b`
        :            //  Else:
         a*b;        //   Return `a*b`
}                    // End of method
replaced http://codegolf.stackexchange.com/ with https://codegolf.stackexchange.com/
Source Link

#Java 7, 49 bytes

corsiKa's answer is exclusivecorsiKa's answer is exclusive, mine is inclusive (and slightly shorter by using recursion instead of a loop).

int q(int a,int b){return a+1!=b?a*q(a+1,b):a*b;}

Ungolfed & test code:

Try it here.

class Main{
    public static void main(String[] a){
        System.out.println(q(2, 5));
        System.out.println(q(5, 10));
        System.out.println(q(-4, 3));
        System.out.println(q(0, 3));
        System.out.println(q(-4, -1));
    }

    static int q(int a, int b){
        return a+1 != b
            ? a * q(a+1, b)
            : a * b;
    }
}

Output:

120
151200
0
0
24

#Java 7, 49 bytes

corsiKa's answer is exclusive, mine is inclusive (and slightly shorter by using recursion instead of a loop).

int q(int a,int b){return a+1!=b?a*q(a+1,b):a*b;}

Ungolfed & test code:

Try it here.

class Main{
    public static void main(String[] a){
        System.out.println(q(2, 5));
        System.out.println(q(5, 10));
        System.out.println(q(-4, 3));
        System.out.println(q(0, 3));
        System.out.println(q(-4, -1));
    }

    static int q(int a, int b){
        return a+1 != b
            ? a * q(a+1, b)
            : a * b;
    }
}

Output:

120
151200
0
0
24

#Java 7, 49 bytes

corsiKa's answer is exclusive, mine is inclusive (and slightly shorter by using recursion instead of a loop).

int q(int a,int b){return a+1!=b?a*q(a+1,b):a*b;}

Ungolfed & test code:

Try it here.

class Main{
    public static void main(String[] a){
        System.out.println(q(2, 5));
        System.out.println(q(5, 10));
        System.out.println(q(-4, 3));
        System.out.println(q(0, 3));
        System.out.println(q(-4, -1));
    }

    static int q(int a, int b){
        return a+1 != b
            ? a * q(a+1, b)
            : a * b;
    }
}

Output:

120
151200
0
0
24
Source Link
Kevin Cruijssen
  • 131.4k
  • 13
  • 144
  • 384
Loading