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:
Find Cube root of a number using Log function
Next article icon

Find Cube root of a number using Log function

Last Updated : 14 Dec, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

Given the number N, the task is to find the cube root using the log function.
Examples:

Input: N = 8 
Output: 2.000000
Input: 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 
=> N(1/3) = d 
Now, apply log on both sides: 
log3 (N(1/3)) = log3 (d) 
=> log3 (d) = 1/3 * log3 (N) 
=> d = 3(1/3 * log3 (N)) 
 


Below is the implementation of the above problem: 

C++
// C++ program to Find Cube root // of a number using Logarithm  #include <bits/stdc++.h>  // Function to find the cube root double cubeRoot(double n) {     // calculate the cube root     double ans = pow(3, (1.0 / 3)                             * (log(n) / log(3)));      // Return the final answer     return ans; }  // Driver code int main() {     double N = 8;      printf("%.2lf ", cubeRoot(N));      return 0; } 
Java
// Java program to Find Cube root // of a number using Logarithm class GFG{       // Function to find the cube root static double cubeRoot(double n) {          // Calculate the cube root     double ans = Math.pow(3, ((1.0 / 3) *                               (Math.log(n) /                                Math.log(3))));      // Return the final answer     return ans; }  // Driver code public static void main(String[] args) {     double N = 8;     System.out.printf("%.2f", cubeRoot(N)); } }  // This code is contributed by Rajput-Ji 
Python3
# Python3 program to find cube root  # of a number using logarithm  import numpy as np  # Function to find the cube root  def cubeRoot(n):      # Calculate the cube root      ans = pow(3, (1.0 / 3) * (np.log(n) /                               np.log(3)))       # Return the final answer      return ans   # Driver code N = 8  print("%.2f" % cubeRoot(N))  # This code is contributed by PratikBasu 
C#
// C# program to find cube root // of a number using logarithm using System;  class GFG{       // Function to find the cube root static double cubeRoot(double n) {          // Calculate the cube root     double ans = Math.Pow(3, ((1.0 / 3) *                               (Math.Log(n) /                                Math.Log(3))));      // Return the readonly answer     return ans; }  // Driver code public static void Main(String[] args) {     double N = 8;          Console.Write("{0:F2}", cubeRoot(N)); } }  // This code is contributed by sapnasingh4991 
JavaScript
<script> // javascript  program to Find Cube root // of a number using Logarithm   // Function to find the cube root function cubeRoot( n) {      // calculate the cube root     let ans = Math.pow(3, (1.0 / 3)                             * (Math.log(n) / Math.log(3)));      // Return the final answer     return ans; }  // Driver code let N = 8; document.write( cubeRoot(N).toFixed(2));      // This code is contributed by todaysgaurav   </script> 

Output
2.00 

Time complexity: O(log2(log3n))
Auxiliary space: O(1)


Next Article
Find Cube root of a number using Log function

S

spp____
Improve
Article Tags :
  • Mathematical
  • Computer Science Fundamentals
  • DSA
  • maths-cube
  • root
Practice Tags :
  • Mathematical

Similar Reads

    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 fin
    3 min read
    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
    Check if a number can be expressed as power | Set 2 (Using Log)
    Check if a number can be expressed as x^y (x raised to power y) Given a positive integer n, find if it can be expressed as x^y where y > 1 and x > 0. x and y both are integers.Examples : Input: n = 8 Output: true 8 can be expressed as 2^3 Input: n = 49 Output: true 49 can be expressed as 7^2 I
    4 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
    Find minimum number of Log value needed to calculate Log upto N
    Given an integer N. The task is to find the minimum number of log values needed to calculate all the log values from 1 to N using properties of the logarithm.Examples: Input : N = 6 Output : 3 Value of log1 is already know, i.e. 0. Except this the three log values needed are, log2, log3, log5. Input
    7 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