Encontrar la raíz cúbica de un número

Dado un número n, encuentre la raíz cúbica de n.
Ejemplos: 
 

Input:  n = 3
Output: Cubic Root is 1.442250

Input: n = 8
Output: Cubic Root is 2.000000

Podemos usar la búsqueda binaria . Primero definimos el error e. Digamos 0.0000001 en nuestro caso. Los pasos principales de nuestro algoritmo para calcular la raíz cúbica de un número n son: 
 

  1. Inicializar inicio = 0 y fin = n
  2. Calcular mid = (inicio + final)/2
  3. Compruebe si el valor absoluto de (n – mid*mid*mid) < e. Si esta condición se cumple, mid es nuestra respuesta, así que devuelve mid. 
     
  4. Si (mid*mid*mid)>n entonces establezca end=mid
  5. Si (mid*mid*mid)<n establece start=mid.

A continuación se muestra la implementación de la idea anterior. 
 

C++

// C++ program to find cubic root of a number
// using Binary Search
#include <bits/stdc++.h>
using namespace std;
 
// Returns the absolute value of n-mid*mid*mid
double diff(double n,double mid)
{
    if (n > (mid*mid*mid))
        return (n-(mid*mid*mid));
    else
        return ((mid*mid*mid) - n);
}
 
// Returns cube root of a no n
double cubicRoot(double n)
{
    // Set start and end for binary search
    double start = 0, end = n;
 
    // Set precision
    double e = 0.0000001;
 
    while (true)
    {
        double mid = (start + end)/2;
        double error = diff(n, mid);
 
        // If error is less than e then mid is
        // our answer so return mid
        if (error <= e)
            return mid;
 
        // If mid*mid*mid is greater than n set
        // end = mid
        if ((mid*mid*mid) > n)
            end = mid;
 
        // If mid*mid*mid is less than n set
        // start = mid
        else
            start = mid;
    }
}
 
// Driver code
int main()
{
    double n = 3;
    printf("Cubic root of %lf is %lf\n",
           n, cubicRoot(n));
    return 0;
}

Java

// Java program to find cubic root of a number
// using Binary Search
import java.io.*;
 
class GFG
{
    // Returns the absolute value of n-mid*mid*mid
    static double diff(double n,double mid)
    {
        if (n > (mid*mid*mid))
            return (n-(mid*mid*mid));
        else
            return ((mid*mid*mid) - n);
    }
     
    // Returns cube root of a no n
    static double cubicRoot(double n)
    {
        // Set start and end for binary search
        double start = 0, end = n;
  
        // Set precision
        double e = 0.0000001;
  
        while (true)
        {
            double mid = (start + end)/2;
            double error = diff(n, mid);
  
            // If error is less than e then mid is
            // our answer so return mid
            if (error <= e)
                return mid;
  
            // If mid*mid*mid is greater than n set
            // end = mid
            if ((mid*mid*mid) > n)
                end = mid;
  
            // If mid*mid*mid is less than n set
            // start = mid
            else
                start = mid;
        }
    }
     
    // Driver program to test above function
    public static void main (String[] args)
    {
        double n = 3;
        System.out.println("Cube root of "+n+" is "+cubicRoot(n));
    }
}
 
// This code is contributed by Pramod Kumar

Python3

# Python 3 program to find cubic root
# of a number using Binary Search
 
# Returns the absolute value of
# n-mid*mid*mid
def diff(n, mid) :
    if (n > (mid * mid * mid)) :
        return (n - (mid * mid * mid))
    else :
        return ((mid * mid * mid) - n)
         
# Returns cube root of a no n
def cubicRoot(n) :
     
    # Set start and end for binary
    # search
    start = 0
    end = n
     
    # Set precision
    e = 0.0000001
    while (True) :
         
        mid = (start + end) / 2
        error = diff(n, mid)
 
        # If error is less than e
        # then mid is our answer
        # so return mid
        if (error <= e) :
            return mid
             
        # If mid*mid*mid is greater
        # than n set end = mid
        if ((mid * mid * mid) > n) :
            end = mid
             
        # If mid*mid*mid is less
        # than n set start = mid
        else :
            start = mid
             
# Driver code
n = 3
print("Cubic root of", n, "is",
      round(cubicRoot(n),6))
 
 
# This code is contributed by Nikita Tiwari.

C#

// C# program to find cubic root
// of a number using Binary Search
using System;
 
class GFG {
     
    // Returns the absolute value
    // of n - mid * mid * mid
    static double diff(double n, double mid)
    {
        if (n > (mid * mid * mid))
            return (n-(mid * mid * mid));
        else
            return ((mid * mid * mid) - n);
    }
     
    // Returns cube root of a no. n
    static double cubicRoot(double n)
    {
         
        // Set start and end for
        // binary search
        double start = 0, end = n;
 
        // Set precision
        double e = 0.0000001;
 
        while (true)
        {
            double mid = (start + end) / 2;
            double error = diff(n, mid);
 
            // If error is less than e then
            // mid is our answer so return mid
            if (error <= e)
                return mid;
 
            // If mid * mid * mid is greater
            // than n set end = mid
            if ((mid * mid * mid) > n)
                end = mid;
 
            // If mid*mid*mid is less than
            // n set start = mid
            else
                start = mid;
        }
    }
     
    // Driver Code
    public static void Main ()
    {
        double n = 3;
        Console.Write("Cube root of "+ n
                       + " is "+cubicRoot(n));
    }
}
 
// This code is contributed by nitin mittal.

PHP

<?php
// PHP program to find cubic root
// of a number using Binary Search
 
// Returns the absolute value
// of n - mid * mid * mid
function diff($n,$mid)
{
    if ($n > ($mid * $mid * $mid))
        return ($n - ($mid *
                $mid * $mid));
    else
        return (($mid * $mid *
                 $mid) - $n);
}
 
// Returns cube root of a no n
function cubicRoot($n)
{
     
    // Set start and end
    // for binary search
    $start = 0;
    $end = $n;
 
    // Set precision
    $e = 0.0000001;
 
    while (true)
    {
        $mid = (($start + $end)/2);
        $error = diff($n, $mid);
 
        // If error is less
        // than e then mid is
        // our answer so return mid
        if ($error <= $e)
            return $mid;
 
        // If mid*mid*mid is
        // greater than n set
        // end = mid
        if (($mid * $mid * $mid) > $n)
            $end = $mid;
 
        // If mid*mid*mid is
        // less than n set
        // start = mid
        else
            $start = $mid;
    }
}
 
    // Driver Code
    $n = 3;
    echo("Cubic root of $n is ");
    echo(cubicRoot($n));
 
// This code is contributed by nitin mittal.
?>

Javascript

<script>
 
// Javascript program to find cubic root of a number
// using Binary Search
 
    // Returns the absolute value of n-mid*mid*mid
    function diff(n, mid)
    {
        if (n > (mid*mid*mid))
            return (n-(mid*mid*mid));
        else
            return ((mid*mid*mid) - n);
    }
       
    // Returns cube root of a no n
    function cubicRoot(n)
    {
        // Set start and end for binary search
        let start = 0, end = n;
    
        // Set precision
        let e = 0.0000001;
    
        while (true)
        {
            let mid = (start + end)/2;
            let error = diff(n, mid);
    
            // If error is less than e then mid is
            // our answer so return mid
            if (error <= e)
                return mid;
    
            // If mid*mid*mid is greater than n set
            // end = mid
            if ((mid*mid*mid) > n)
                end = mid;
    
            // If mid*mid*mid is less than n set
            // start = mid
            else
                start = mid;
        }
    }
   
 
// Driver Code
     
        let n = 3;
        document.write("Cube root of "+n+" is "+cubicRoot(n));
         
</script>

Producción: 

Cubic root of 3.000000 is 1.442250

Complejidad de tiempo: O (Iniciar sesión)
Este artículo es una contribución de Madhur Modi . Si te gusta GeeksforGeeks y te gustaría contribuir, también puedes escribir un artículo usando write.geeksforgeeks.org o enviar tu artículo por correo a review-team@geeksforgeeks.org . Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.
Escriba comentarios si encuentra algo incorrecto o si desea compartir más información sobre el tema tratado anteriormente.
 

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *