Nth root of a number using log Last Updated : 18 Sep, 2022 Comments Improve Suggest changes Like Article Like Report Given two integers N and K, the task is to find the Nth root of the K. Examples: Input: N = 3, K = 8 Output: 2.00 Explanation: Cube root of 8 is 2. i.e. 23 = 8 Input: N = 2, K = 16 Output: 4.00 Explanation: Square root of 16 is 4, i.e. 42 = 16 Approach: The idea is to use logarithmic function to find the Nth root of K. Let D be our Nth root of the K, Then, N^{\frac{1}{K}} = D Apply logK on both the sides - => log_{K}(N^{\frac{1}{K}}) = log_{K}(D) => \frac{1}{K} * log_{K}(N) = log_{K}(D) => D = K^{\frac{1}{K} * log_{K}(N)} Below is the implementation of the above approach: C++ // C++ implementation to find the // Kth root of a number using log #include <bits/stdc++.h> // Function to find the Kth root // of the number using log function double kthRoot(double n, int k) { return pow(k, (1.0 / k) * (log(n) / log(k))); } // Driver Code int main(void) { double n = 81; int k = 4; printf("%lf ", kthRoot(n, k)); return 0; } Java // Java implementation to find the // Kth root of a number using log import java.util.*; class GFG { // Function to find the Kth root // of the number using log function static double kthRoot(double n, int k) { return Math.pow(k, ((1.0 / k) * (Math.log(n) / Math.log(k)))); } // Driver Code public static void main(String args[]) { double n = 81; int k = 4; System.out.printf("%.6f", kthRoot(n, k)); } } // This code is contributed by rutvik_56 Python3 # Python3 implementation to find the # Kth root of a number using log import numpy as np # Function to find the Kth root # of the number using log function def kthRoot(n, k): return pow(k, ((1.0 / k) * (np.log(n) / np.log(k)))) # Driver Code n = 81 k = 4 print("%.6f" % kthRoot(n, k)) # This code is contributed by PratikBasu C# // C# implementation to find the // Kth root of a number using log using System; class GFG { // Function to find the Kth root // of the number using log function static double kthRoot(double n, int k) { return Math.Pow(k, ((1.0 / k) * (Math.Log(n) / Math.Log(k)))); } // Driver Code public static void Main(String []args) { double n = 81; int k = 4; Console.Write("{0:F6}", kthRoot(n, k)); } } // This code is contributed by AbhiThakur JavaScript <script> // Javascript implementation to find the // Kth root of a number using log // Function to find the Kth root // of the number using log function function kthRoot(n, k) { return Math.pow(k, ((1.0 / k) * (Math.log(n) / Math.log(k)))); } // Driver Code var n = 81; var k = 4; var x = kthRoot(n, k) document.write(x.toFixed(6)); // This code is contributed by Ankita saini </script> Output: 3.000000 Time Complexity: O(1) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Nth root of a number using log S spp____ Follow Improve Article Tags : Mathematical DSA root Practice Tags : Mathematical Similar Reads Square root of a number using log For a given number find the square root using log function. Number may be int, float or double. Examples: Input : n = 9Output : 3 Input : n = 2.93Output : 1.711724 We can find square root of a number using sqrt() method: C++ // C++ program to demonstrate finding // square root of a number using sqrt 3 min read N-th root of a number Given two numbers n and m, find the n-th root of m. In mathematics, the n-th root of a number m is a real number that, when raised to the power of n, gives m. If no such real number exists, return -1.Examples: Input: n = 2, m = 9Output: 3Explanation: 32 = 9Input: n = 3, m = 9Output: -1Explanation: 3 9 min read Fifth root of a number Given a number, print floor of 5'th root of the number.Examples: Input : n = 32 Output : 2 2 raise to power 5 is 32 Input : n = 250 Output : 3 Fifth square root of 250 is between 3 and 4 So floor value is 3. Method 1 (Simple) A simple solution is initialize result as 0, keep incrementing result whil 10 min read Find Cube root of a number using Log function Given the number N, the task is to find the cube root using the log function.Examples: Input: N = 8 Output: 2.000000Input: N = 27 Output: 3.000000 Approach: To solve the problem mentioned above we will use log() function, according to the following formula: Let cube root of N be d. => ?N = d = 3 min read Number of Digits in a^b Given two positive integers a and b, task is to find the number of digits in a^b (a raised to the power b).Example: Input: a = 2 b = 5 Output: no. of digits = 2 Explanation: 2^5 = 32 Hence, no. of digits = 2 Input: a = 2 b = 100 Output: no. of digits = 31 Explanation: 2^100 = 1.2676506e+30 Hence, no 4 min read Like