logo

Descubra la potencia sin utilizar la función POW en C

La función pow() se utiliza para calcular la potencia de un número entero dado. Ahora en este artículo vamos a entender con la ayuda de un programa cómo calcular la potencia de un número entero sin usar la función pow() en C.

Uso del bucle for para determinar la potencia de un entero dado

Imagina que necesitas localizar a ^ b. El método más sencillo es multiplicar a por b veces usando un bucle.

  • Sea a ^ b la entrada. La base es a, mientras que el exponente es b.
  • Comience con una potencia de 1.
  • Usando un bucle, ejecute las siguientes instrucciones b veces
  • potencia = potencia * a
  • El sistema eléctrico tiene la solución final, a^b.

Entendamos mejor el enfoque anterior con un ejemplo de un programa en C:

 # include # include # include # include # include int Pow ( int a , int b ) { int power = 1 , i ; for ( i = 1 ; i <= b ; + i ) { power="power" * a } return int main ( long base , exponent printf ' enter : scanf % d & ^ pow < pre> <p> <strong>Output:</strong> </p> <pre> Enter Base: 5 Enter Power: 3 5 ^ 3 = 125 .......................... Process executed in 3.22 seconds Press any key to continue. </pre> <p> <strong>Explanation</strong> </p> <p>The code above has an O (N) time complexity, where N is the exponent. O is the space complexity (1).</p> <h3>Using While loop:</h3> <pre> # include # include # include # include # include int main ( ) { int n , exp , exp1 ; long long int value = 1 ; printf ( &apos; enter the number and its exponential :  n  n &apos; ) ; scanf ( &apos; % d % d &apos; , &amp; n , &amp; exp ) ; exp1 = exp ; // storing original value for future use // same as while ( ( - - exp ) ! = - 1 ) while ( exp - - &gt; 0 ) { value * = n ; // multiply n to itself exp times } printf ( &apos;  n  n % d ^ % d = % l l d  n  n &apos; , n , exp1 , value ) ; return 0; } </pre> <p> <strong>Output:</strong> </p> <pre> enter the number and its exponential : 5 4 5 ^ 6 = 625 .......................... Process executed in 0.11 seconds Press any key to continue. </pre> <p> <strong>Explanation</strong> </p> <p>Long Long Int is twice as large as Long Int. The format specifier for long long int is percent lld.</p> <h2>Using Recursion to find the Power of Given Integer</h2> <p>Assume that a ^ b is the input. The power of &apos;a&apos; will increase by one with each recursive call. To obtain a ^ b, we call the recursive function b twice.</p> <ul> <li>Let Pow ( a, b ) be the recursive function used to calculate a ^ b.</li> <li>Simply return 1 if b == 0; else, return Pow (a, b -1) * a.</li> </ul> <p> <strong>Let&apos;s understand the above approach better with an example of a program in C:</strong> </p> <pre> # include # include # include # include # include int Pow ( int a , int b ) { if ( b = = 0 ) return 1 ; else return Pow ( a , b - 1 ) * X ; } int main ( ) { long long int base , exponent ; printf ( &apos; enter Base : &apos; ) ; scanf ( &apos; % d &apos; , &amp; base ) ; printf ( &apos; enter Power : &apos; ) ; scanf ( &apos; % d &apos; , &amp; exponent ) ; printf ( &apos; % d ^ % d = % d &apos; , base , exponent , Pow ( base , exponent ) ) ; return 0; } </pre> <p> <strong>Output:</strong> </p> <pre> Enter Base: 5 Enter Power: 4 5 ^ 4 = 625 .......................... Process executed in 1.22 seconds Press any key to continue. </pre> <p> <strong>Explanation:</strong> </p> <p>In the above example of a code in C, time complexity would be exponent N, O(N) &amp; O(N) space complexity, internal stack.</p> <hr></=>

Explicación

la cadena java contiene

El código anterior tiene una complejidad temporal O (N), donde N es el exponente. O es la complejidad espacial (1).

Usando el bucle While:

 # include # include # include # include # include int main ( ) { int n , exp , exp1 ; long long int value = 1 ; printf ( &apos; enter the number and its exponential :  n  n &apos; ) ; scanf ( &apos; % d % d &apos; , &amp; n , &amp; exp ) ; exp1 = exp ; // storing original value for future use // same as while ( ( - - exp ) ! = - 1 ) while ( exp - - &gt; 0 ) { value * = n ; // multiply n to itself exp times } printf ( &apos;  n  n % d ^ % d = % l l d  n  n &apos; , n , exp1 , value ) ; return 0; } 

Producción:

 enter the number and its exponential : 5 4 5 ^ 6 = 625 .......................... Process executed in 0.11 seconds Press any key to continue. 

Explicación

Long Long Int es dos veces más grande que Long Int. El especificador de formato para long long int es percent lld.

reiniciar mysql ubuntu

Usando la recursividad para encontrar la potencia de un entero dado

Supongamos que a ^ b es la entrada. El poder de 'a' aumentará en uno con cada llamada recursiva. Para obtener a ^ b, llamamos a la función recursiva b dos veces.

convertir cadena a json en java
  • Sea Pow ( a, b ) la función recursiva utilizada para calcular a ^ b.
  • Simplemente devuelve 1 si b == 0; de lo contrario, devuelva Pow (a, b -1) * a.

Entendamos mejor el enfoque anterior con un ejemplo de un programa en C:

 # include # include # include # include # include int Pow ( int a , int b ) { if ( b = = 0 ) return 1 ; else return Pow ( a , b - 1 ) * X ; } int main ( ) { long long int base , exponent ; printf ( &apos; enter Base : &apos; ) ; scanf ( &apos; % d &apos; , &amp; base ) ; printf ( &apos; enter Power : &apos; ) ; scanf ( &apos; % d &apos; , &amp; exponent ) ; printf ( &apos; % d ^ % d = % d &apos; , base , exponent , Pow ( base , exponent ) ) ; return 0; } 

Producción:

 Enter Base: 5 Enter Power: 4 5 ^ 4 = 625 .......................... Process executed in 1.22 seconds Press any key to continue. 

Explicación:

En el ejemplo anterior de un código en C, la complejidad temporal sería el exponente N, O(N) y O(N) de complejidad espacial, pila interna.