Skip to main content
Commonmark migration
Source Link

#Java 7, 47 bytes

Java 7, 47 bytes

int c(double n){return n>1?1+c(Math.log(n)):0;}

Try it online.

The recursive Java 7 style method above is 2 bytes shorter than an iterative Java 8 style lambda:

n->{int c=0;for(;n>1;c++)n=Math.log(n);return c;}

Try it online.

Explanation:

int c(double n){      // Method with double parameter and integer return-type
  return n>1?         //  If the input is larger than 1:
    1+                //   Return 1 +
      c(Math.log(n))  //   A recursive call with log(input)
   :                  //  Else:
    0;                //   Return 0 instead

n->{                  // Method with double parameter and integer return-type
  int c=0;            //  Create a counter, starting at 0
  for(;n>1;           //  Loop as long as the input is still larger than 1:
    c++)              //   Increase the counter by 1
    n=Math.log(n);    //   And update the input to log(input)
  return c;}          //  After the loop: return the counter as result

#Java 7, 47 bytes

int c(double n){return n>1?1+c(Math.log(n)):0;}

Try it online.

The recursive Java 7 style method above is 2 bytes shorter than an iterative Java 8 style lambda:

n->{int c=0;for(;n>1;c++)n=Math.log(n);return c;}

Try it online.

Explanation:

int c(double n){      // Method with double parameter and integer return-type
  return n>1?         //  If the input is larger than 1:
    1+                //   Return 1 +
      c(Math.log(n))  //   A recursive call with log(input)
   :                  //  Else:
    0;                //   Return 0 instead

n->{                  // Method with double parameter and integer return-type
  int c=0;            //  Create a counter, starting at 0
  for(;n>1;           //  Loop as long as the input is still larger than 1:
    c++)              //   Increase the counter by 1
    n=Math.log(n);    //   And update the input to log(input)
  return c;}          //  After the loop: return the counter as result

Java 7, 47 bytes

int c(double n){return n>1?1+c(Math.log(n)):0;}

Try it online.

The recursive Java 7 style method above is 2 bytes shorter than an iterative Java 8 style lambda:

n->{int c=0;for(;n>1;c++)n=Math.log(n);return c;}

Try it online.

Explanation:

int c(double n){      // Method with double parameter and integer return-type
  return n>1?         //  If the input is larger than 1:
    1+                //   Return 1 +
      c(Math.log(n))  //   A recursive call with log(input)
   :                  //  Else:
    0;                //   Return 0 instead

n->{                  // Method with double parameter and integer return-type
  int c=0;            //  Create a counter, starting at 0
  for(;n>1;           //  Loop as long as the input is still larger than 1:
    c++)              //   Increase the counter by 1
    n=Math.log(n);    //   And update the input to log(input)
  return c;}          //  After the loop: return the counter as result
added 911 characters in body
Source Link
Kevin Cruijssen
  • 131.4k
  • 13
  • 144
  • 384
int c(double n){return n>1?1+c(Math.log(n)):0;}
int c(double n){return n>1?1+c(Math.log(n)):0;}

Ungolfed & test cases:Try it online.

Try it here. The recursive Java 7 style method above is 2 bytes shorter than an iterative Java 8 style lambda:

class Main{
  static int c(double n){
    return n > 1
             ? 1 + c(Math.log(n))
             : 0;
  }

  public static void main(String[] a){
    System.out.println(c(0));
    System.out.println(c(1));
    System.out.println(c(2));
    System.out.println(c(3));
    System.out.println(c(4));
    System.out.println(c(15));
    System.out.println(c(16));
    System.out.println(c(3814279));
    System.out.println(c(3814280));
  }
}
n->{int c=0;for(;n>1;c++)n=Math.log(n);return c;}

Try it online.

OutputExplanation:

0
0
1
2
2
2
3
3
4
int c(double n){      // Method with double parameter and integer return-type
  return n>1?         //  If the input is larger than 1:
    1+                //   Return 1 +
      c(Math.log(n))  //   A recursive call with log(input)
   :                  //  Else:
    0;                //   Return 0 instead

n->{                  // Method with double parameter and integer return-type
  int c=0;            //  Create a counter, starting at 0
  for(;n>1;           //  Loop as long as the input is still larger than 1:
    c++)              //   Increase the counter by 1
    n=Math.log(n);    //   And update the input to log(input)
  return c;}          //  After the loop: return the counter as result
int c(double n){return n>1?1+c(Math.log(n)):0;}

Ungolfed & test cases:

Try it here.

class Main{
  static int c(double n){
    return n > 1
             ? 1 + c(Math.log(n))
             : 0;
  }

  public static void main(String[] a){
    System.out.println(c(0));
    System.out.println(c(1));
    System.out.println(c(2));
    System.out.println(c(3));
    System.out.println(c(4));
    System.out.println(c(15));
    System.out.println(c(16));
    System.out.println(c(3814279));
    System.out.println(c(3814280));
  }
}

Output:

0
0
1
2
2
2
3
3
4
int c(double n){return n>1?1+c(Math.log(n)):0;}

Try it online.

The recursive Java 7 style method above is 2 bytes shorter than an iterative Java 8 style lambda:

n->{int c=0;for(;n>1;c++)n=Math.log(n);return c;}

Try it online.

Explanation:

int c(double n){      // Method with double parameter and integer return-type
  return n>1?         //  If the input is larger than 1:
    1+                //   Return 1 +
      c(Math.log(n))  //   A recursive call with log(input)
   :                  //  Else:
    0;                //   Return 0 instead

n->{                  // Method with double parameter and integer return-type
  int c=0;            //  Create a counter, starting at 0
  for(;n>1;           //  Loop as long as the input is still larger than 1:
    c++)              //   Increase the counter by 1
    n=Math.log(n);    //   And update the input to log(input)
  return c;}          //  After the loop: return the counter as result
Source Link
Kevin Cruijssen
  • 131.4k
  • 13
  • 144
  • 384

#Java 7, 47 bytes

int c(double n){return n>1?1+c(Math.log(n)):0;}

Ungolfed & test cases:

Try it here.

class Main{
  static int c(double n){
    return n > 1
             ? 1 + c(Math.log(n))
             : 0;
  }

  public static void main(String[] a){
    System.out.println(c(0));
    System.out.println(c(1));
    System.out.println(c(2));
    System.out.println(c(3));
    System.out.println(c(4));
    System.out.println(c(15));
    System.out.println(c(16));
    System.out.println(c(3814279));
    System.out.println(c(3814280));
  }
}

Output:

0
0
1
2
2
2
3
3
4