Dados dos enteros N y K , la tarea es encontrar la raíz N -ésima de K.
Ejemplos:
Entrada: N = 3, K = 8
Salida: 2,00
Explicación:
La raíz cúbica de 8 es 2, es decir, 2 3 = 8Entrada: N = 2, K = 16
Salida: 4,00
Explicación:
La raíz cuadrada de 16 es 4, es decir, 4 2 = 16
Enfoque: La idea es usar la función logarítmica para encontrar la raíz N de K.
Sea D nuestra raíz N-ésima de K, luego
aplique
log K en ambos lados –
=>
=>
=>
A continuación se muestra la implementación del enfoque anterior:
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>
Producción:
3.000000