Nth root of a number using log

08 Apr, 2021
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, Apply logK on both the sides – => => =>  
Below is the implementation of the above approach:  
C++
Java
Python3
C#
Javascript
// 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 functiondouble kthRoot(double n, int k){    return pow(k,               (1.0 / k)                   * (log(n)                      / log(k)));} // Driver Codeint main(void){    double n = 81;    int k = 4;    printf("%lf ", kthRoot(n, k));    return 0;}
// Java implementation to find the// Kth root of a number using logimport java.util.*; class GFG { // Function to find the Kth root// of the number using log functionstatic double kthRoot(double n, int k){    return Math.pow(k, ((1.0 / k) *                       (Math.log(n) /                        Math.log(k))));} // Driver Codepublic 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 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 functiondef kthRoot(n, k):         return pow(k, ((1.0 / k) *                  (np.log(n) /                   np.log(k))))                    # Driver Coden = 81k = 4 print("%.6f" % kthRoot(n, k)) # This code is contributed by PratikBasu   
// C# implementation to find the// Kth root of a number using logusing System; class GFG { // Function to find the Kth root// of the number using log functionstatic double kthRoot(double n, int k){         return Math.Pow(k, ((1.0 / k) *                        (Math.Log(n) /                         Math.Log(k))));} // Driver Codepublic static void Main(String []args){    double n = 81;    int k = 4;         Console.Write("{0:F6}", kthRoot(n, k));}} // This code is contributed by AbhiThakur
<script> // Javascript implementation to find the// Kth root of a number using log // Function to find the Kth root// of the number using log functionfunction kthRoot(n, k){   return Math.pow(k, ((1.0 / k) *                       (Math.log(n) /                        Math.log(k))));}  // Driver Codevar n = 81;var k = 4;var x = kthRoot(n, k) document.write(x.toFixed(6)); // This code is contributed by Ankita saini                     </script>
3.000000
 
PratikBasu
rutvik_56
abhaysingh290895
ankita_saini
root
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Algorithm to solve Rubik's Cube
Program to print prime numbers from 1 to N.
Merge two sorted arrays with O(1) extra space
Segment Tree | Set 1 (Sum of given range)
Fizz Buzz Implementation
Check if a number is Palindrome
Count ways to reach the n'th stair
Product of Array except itself
Find Union and Intersection of two unsorted arrays
Median of two sorted arrays of same size