Dados los números enteros positivos N y K , la tarea es encontrar la potencia más alta y más pequeña de K mayor que igual a y menor que igual a N respectivamente.
Ejemplos:
Entrada: N = 3, K = 2
Salida: 2 4
Potencia más alta de 2 menor que 3 = 2
Potencia más pequeña de 2 mayor que 3 = 4Entrada: N = 6, K = 3
Salida: 3 9
Potencia más alta de 3 menor que 6 = 3
Potencia más pequeña de 3 mayor que 6 = 9
Acercarse:
- Calcule el logaritmo de N en base K ( log K N ) para obtener la potencia exponencial tal que K elevado a este exponente es la potencia más alta de K menor que igual a N.
- Para la potencia más pequeña de K menor que igual a N, encuentre la siguiente potencia de K calculada a partir del último paso
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the highest power of k less than or // equal to n int prevPowerofK(int n, int k) { int p = (int)(log(n) / log(k)); return (int)pow(k, p); } // Function to return the smallest power of k greater than // or equal to n int nextPowerOfK(int n, int k) { return prevPowerofK(n, k) * k; } // Function to print the result void printResult(int n, int k) { cout << prevPowerofK(n, k) << " " << nextPowerOfK(n, k) << endl; } // Driver code int main() { int n = 25, k = 3; printResult(n, k); return 0; } // This code is contributed by Sania Kumari Gupta
C
// C implementation of the approach #include <math.h> #include <stdio.h> // Function to return the highest power of k less than or // equal to n int prevPowerofK(int n, int k) { int p = (int)(log(n) / log(k)); return (int)pow(k, p); } // Function to return the smallest power of k greater than // or equal to n int nextPowerOfK(int n, int k) { return prevPowerofK(n, k) * k; } // Function to print the result void printResult(int n, int k) { printf("%d %d\n", prevPowerofK(n, k), nextPowerOfK(n, k)); } // Driver code int main() { int n = 25, k = 3; printResult(n, k); return 0; } // This code is contributed by Sania Kumari Gupta
Java
// Java implementation of the approach import java.io.*; class GFG{ // Function to return the highest power // of k less than or equal to n static int prevPowerofK(int n, int k) { int p = (int)(Math.log(n) / Math.log(k)); return (int) Math.pow(k, p); } // Function to return the smallest power // of k greater than or equal to n static int nextPowerOfK(int n, int k) { return prevPowerofK(n, k) * k; } // Function to print the result static void printResult(int n, int k) { System.out.println(prevPowerofK(n, k) + " " + nextPowerOfK(n, k)); } // Driver Code public static void main (String args[]) { int n = 25, k = 3; printResult(n, k); } } // This code is contributed by shivanisinghss2110
Python3
# Python3 implementation of the approach import math # Function to return the highest power # of k less than or equal to n def prevPowerofK(n, k): p = int(math.log(n) / math.log(k)) return int(math.pow(k, p)) # Function to return the smallest power # of k greater than or equal to n def nextPowerOfK(n, k): return prevPowerofK(n, k) * k # Function to print the result def printResult(n, k): print(prevPowerofK(n, k), nextPowerOfK(n, k)) # Driver code n = 6 k = 3 printResult(n, k) # This code is contributed by divyamohan123
C#
// C# implementation of the approach using System; class GFG{ // Function to return the highest power // of k less than or equal to n static int prevPowerofK(int n, int k) { int p = (int)(Math.Log(n) / Math.Log(k)); return (int) Math.Pow(k, p); } // Function to return the smallest power // of k greater than or equal to n static int nextPowerOfK(int n, int k) { return prevPowerofK(n, k) * k; } // Function to print the result static void printResult(int n, int k) { Console.WriteLine(prevPowerofK(n, k) + " " + nextPowerOfK(n, k)); } // Driver Code public static void Main(String []args) { int n = 25, k = 3; printResult(n, k); } } // This code is contributed by gauravrajput1
Javascript
<script> // Javascript implementation of the approach // Function to return the highest power // of k less than or equal to n function prevPowerofK(n, k) { var p = parseInt(Math.log(n) / Math.log(k)); return parseInt(Math.pow(k, p)); } // Function to return the smallest power // of k greater than or equal to n function nextPowerOfK(n, k) { return prevPowerofK(n, k) * k; } // Function to print the result function printResult(n, k) { document.write(prevPowerofK(n, k) + " " + nextPowerOfK(n, k) + "<br>"); } // Driver code var n = 25, k = 3; printResult(n, k); </script>
Producción:
9 27
Complejidad de tiempo: O (log k n), ya que la función de potencia incorporada costará O (log k n) tiempo.
Espacio auxiliar: O(1), ya que no estamos utilizando ningún espacio adicional.