Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DSA
  • Practice Mathematical Algorithm
  • Mathematical Algorithms
  • Pythagorean Triplet
  • Fibonacci Number
  • Euclidean Algorithm
  • LCM of Array
  • GCD of Array
  • Binomial Coefficient
  • Catalan Numbers
  • Sieve of Eratosthenes
  • Euler Totient Function
  • Modular Exponentiation
  • Modular Multiplicative Inverse
  • Stein's Algorithm
  • Juggler Sequence
  • Chinese Remainder Theorem
  • Quiz on Fibonacci Numbers
Open In App
Next Article:
Nth root of a number using log
Next article icon

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)


Next Article
Nth root of a number using log

S

spp____
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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences