Nth root of a number using log

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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    

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output:

3.000000

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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.