Write a one line C function that calculates and returns
. For example, if n = 64, then your function should return 6, and if n = 129, then your function should return 7.
Using Recursion
C
// C program to find log(n) using Recursion #include <stdio.h> unsigned int Log2n(unsigned int n) { return (n > 1) ? 1 + Log2n(n / 2) : 0; } int main() { unsigned int n = 32; printf("%u", Log2n(n)); getchar(); return 0; } |
Java
// Java program to find log(n) // using Recursion class Gfg1 { static int Log2n(int n) { return (n > 1) ? 1 + Log2n(n / 2) : 0; } // Driver Code public static void main(String args[]) { int n = 32; System.out.println(Log2n(n)); } } // This code is contributed by Niraj_Pandey |
Python3
# Python 3 program to # find log(n) using Recursion def Log2n(n): return 1 + Log2n(n / 2) if (n > 1) else 0 # Driver code n = 32print(Log2n(n)) # This code is contributed by # Smitha Dinesh Semwal |
C#
// C# program to find log(n) // using Recursion using System; class GFG { static int Log2n(int n) { return (n > 1) ? 1 + Log2n(n / 2) : 0; } // Driver Code public static void Main() { int n = 32; Console.Write(Log2n(n)); } } // This code is contributed by // nitin mittal. |
Output :
5
Time complexity: O(log n)
Auxiliary space: O(log n) if the stack size is considered during recursion otherwise O(1)
Using inbuilt log function
We can use the inbuilt function of standard library which is available in library.
C
// C program to find log(n) using Inbuilt // function of <math.h> library #include <math.h> #include <stdio.h> int main() { unsigned int n = 32; printf("%d", (int)log2(n)); return 0; } |
Java
// Java program to find log(n) using Inbuilt // function of java.util.Math library import java.util.*; class Gfg2 { public static void main(String args[]) { int n = 32; System.out.println((int)(Math.log(n) / Math.log(2))); } } // This code is contributed by Niraj_Pandey |
Output :
5
Time complexity: O(1)
Auxiliary space: O(1)
Let us try an extended version of the problem.
Write a one line function Logn(n, r) which returns
.
Using Recursion
C
// C program to find log(n) on arbitrary base using Recursion #include <stdio.h> unsigned int Logn(unsigned int n, unsigned int r) { return (n > r - 1) ? 1 + Logn(n / r, r) : 0; } int main() { unsigned int n = 256; unsigned int r = 3; printf("%u", Logn(n, r)); return 0; } |
Java
// Java program to find log(n) on // arbitrary base using Recursion class Gfg3 { static int Logn(int n, int r) { return (n > r - 1) ? 1 + Logn(n / r, r) : 0; } // Driver Code public static void main(String args[]) { int n = 256; int r = 3; System.out.println(Logn(n, r)); } } // This code is contributed by Niraj_Pandey |
Output :
5
Time complexity: O(log n)
Auxiliary space: O(log n) if the stack size is considered during recursion otherwise O(1)
Using inbuilt log function
We only need to use logarithm property to find the value of log(n) on arbitrary base r. i.e.,
where k can be any anything, which for standard log functions are either e or 10
C
// C program to find log(n) on arbitrary base // using log() function of maths library #include <math.h> #include <stdio.h> unsigned int Logn(unsigned int n, unsigned int r) { return log(n) / log(r); } int main() { unsigned int n = 256; unsigned int r = 3; printf("%u", Logn(n, r)); return 0; } |
Java
// Java program to find log(n) on arbitrary base // using log() function of java.util.Math library import java.util.*; class Gfg4 { public static void main(String args[]) { int n = 256; int r = 3; System.out.println((int)(Math.log(n) / Math.log(r))); } } // This code is contributed by Niraj_Pandey |
Output :
5
Time complexity: O(1)
Auxiliary space: O(1)
This article is contributed by Shubham Bansal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- C Program to Compute Quotient and Remainder
- log() function in C++
- valarray log() function in C++
- C program to detect tokens in a C program
- C program to print a string without any quote (singe or double) in the program
- Hello World Program : First program while learning Programming
- Program for Sum of the digits of a given number
- Write a C program to print "Geeks for Geeks" without using a semicolon
- Output of the program | Dereference, Reference, Dereference, Reference....
- Quine - A self-reproducing program
- Output of C++ Program | Set 10
- Output of C++ Program | Set 14
- Output of C++ Program | Set 16
- Output of C++ Program | Set 18
- Program to print last 10 lines
- Write a C program that does not terminate when Ctrl+C is pressed
- Write a C program that won't compile in C++
- Write a program that produces different results in C and C++
- OpenCV C++ Program to play a video
- OpenCV C++ Program to blur a Video

