Escriba su propio Poder sin usar operadores de multiplicación (*) y división (/)

Método 1 (usando bucles anidados)
Podemos calcular la potencia usando sumas repetidas. 
Por ejemplo para calcular 5^6. 
1) Primero 5 veces sumamos 5, obtenemos 25. (5^2) 
2) Luego 5 veces sumamos 25, obtenemos 125. (5^3) 
3) Luego 5 veces sumamos 125, obtenemos 625 (5^4) 
4) Luego 5 veces sumamos 625, obtenemos 3125 (5^5) 
5) Luego 5 veces sumamos 3125, obtenemos 15625 (5^6) 

C++

// C++ code for power function
#include <bits/stdc++.h>
using namespace std;
 
/* Works only if a >= 0 and b >= 0 */
int pow(int a, int b)
{
    if (b == 0)
        return 1;
    int answer = a;
    int increment = a;
    int i, j;
    for(i = 1; i < b; i++)
    {
        for(j = 1; j < a; j++)
        {
            answer += increment;
        }
        increment = answer;
    }
    return answer;
}
 
// Driver Code
int main()
{
    cout << pow(5, 3);
    return 0;
}
 
// This code is contributed
// by rathbhupendra

C

#include<stdio.h>
/* Works only if a >= 0 and b >= 0  */
int pow(int a, int b)
{
  //base case : anything raised to the power 0 is 1
  if (b == 0)
    return 1;
  int answer = a;
  int increment = a;
  int i, j;
  for(i = 1; i < b; i++)
  {
     for(j = 1; j < a; j++)
     {
        answer += increment;
     }
     increment = answer;
  }
  return answer;
}
 
/* driver program to test above function */
int main()
{
  printf("\n %d", pow(5, 3));
  getchar();
  return 0;
}

Java

import java.io.*;
 
class GFG {
     
    /* Works only if a >= 0 and b >= 0 */
    static int pow(int a, int b)
    {
        if (b == 0)
            return 1;
             
        int answer = a;
        int increment = a;
        int i, j;
         
        for (i = 1; i < b; i++) {
            for (j = 1; j < a; j++) {
                answer += increment;
            }
            increment = answer;
        }
         
        return answer;
    }
 
    // driver program to test above function
    public static void main(String[] args)
    {
        System.out.println(pow(5, 3));
    }
}
 
// This code is contributed by vt_m.

Python

# Python 3 code for power
# function
 
# Works only if a >= 0 and b >= 0
def pow(a,b):
    if(b==0):
        return 1
         
    answer=a
    increment=a
     
    for i in range(1,b):
        for j in range (1,a):
            answer+=increment
        increment=answer
    return answer
 
# driver code
print(pow(5,3))
 
# this code is contributed
# by Sam007

C#

using System;
 
class GFG
{
    /* Works only if a >= 0 and b >= 0 */
    static int pow(int a, int b)
    {
        if (b == 0)
            return 1;
             
        int answer = a;
        int increment = a;
        int i, j;
         
        for (i = 1; i < b; i++) {
            for (j = 1; j < a; j++) {
                answer += increment;
            }
            increment = answer;
        }
         
        return answer;
    }
 
    // driver program to test
    // above function
    public static void Main()
    {
        Console.Write(pow(5, 3));
    }
}
 
// This code is contributed by Sam007

PHP

<?php
 
// Works only if a >= 0
// and b >= 0
function poww($a, $b)
{
    if ($b == 0)
        return 1;
    $answer = $a;
    $increment = $a;
    $i;
    $j;
    for($i = 1; $i < $b; $i++)
    {
        for($j = 1; $j < $a; $j++)
        {
            $answer += $increment;
        }
        $increment = $answer;
    }
    return $answer;
}
 
    // Driver Code
    echo( poww(5, 3));
  
// This code is contributed by nitin mittal.
?>

Javascript

<script>
    /* Works only if a >= 0 and b >= 0 */
function pow(a , b)
{
    if (b == 0)
        return 1;
         
    var answer = a;
    var increment = a;
    var i, j;
     
    for (i = 1; i < b; i++)
    {
        for (j = 1; j < a; j++)
        {
            answer += increment;
        }
        increment = answer;
    }
     
    return answer;
}
 
// driver program to test above function
document.write(pow(5, 3));
 
// This code is contributed by shikhasingrajput
</script>

Producción : 

C++

#include<bits/stdc++.h>
using namespace std;
 
/* A recursive function to get x*y */
int multiply(int x, int y)
{
    if(y)
        return (x + multiply(x, y - 1));
    else
        return 0;
}
 
/* A recursive function to get a^b
Works only if a >= 0 and b >= 0 */
int pow(int a, int b)
{
    if(b)
        return multiply(a, pow(a, b - 1));
    else
        return 1;
}
 
// Driver Code
int main()
{
    cout << pow(5, 3);
    getchar();
    return 0;
}
 
// This code is contributed
// by Akanksha Rai

C

#include<stdio.h>
/* A recursive function to get a^b
  Works only if a >= 0 and b >= 0  */
int pow(int a, int b)
{
   if(b)
     return multiply(a, pow(a, b-1));
   else
    return 1;
}   
 
/* A recursive function to get x*y */
int multiply(int x, int y)
{
   if(y)
     return (x + multiply(x, y-1));
   else
     return 0;
}
 
/* driver program to test above functions */
int main()
{
  printf("\n %d", pow(5, 3));
  getchar();
  return 0;
}

Java

import java.io.*;
 
class GFG {
     
    /* A recursive function to get a^b
    Works only if a >= 0 and b >= 0 */
    static int pow(int a, int b)
    {
         
        if (b > 0)
            return multiply(a, pow(a, b - 1));
        else
            return 1;
    }
 
    /* A recursive function to get x*y */
    static int multiply(int x, int y)
    {
         
        if (y > 0)
            return (x + multiply(x, y - 1));
        else
            return 0;
    }
 
    /* driver program to test above functions */
    public static void main(String[] args)
    {
        System.out.println(pow(5, 3));
    }
}
 
// This code is contributed by vt_m.

Python3

def pow(a,b):
     
    if(b):
        return multiply(a, pow(a, b-1));
    else:
        return 1;
      
# A recursive function to get x*y *
def multiply(x, y):
     
    if (y):
        return (x + multiply(x, y-1));
    else:
        return 0;
 
# driver program to test above functions *
print(pow(5, 3));
 
 
# This code is contributed
# by Sam007

C#

using System;
 
class GFG
{
    /* A recursive function to get a^b
    Works only if a >= 0 and b >= 0 */
    static int pow(int a, int b)
    {
         
        if (b > 0)
            return multiply(a, pow(a, b - 1));
        else
            return 1;
    }
 
    /* A recursive function to get x*y */
    static int multiply(int x, int y)
    {
         
        if (y > 0)
            return (x + multiply(x, y - 1));
        else
            return 0;
    }
 
    /* driver program to test above functions */
    public static void Main()
    {
        Console.Write(pow(5, 3));
    }
}
 
// This code is contributed by Sam007

PHP

<?php
 
/* A recursive function to get a^b
   Works only if a >= 0 and b >= 0 */
function p_ow( $a, $b)
{
    if($b)
        return multiply($a,
          p_ow($a, $b - 1));
    else
        return 1;
}
 
/* A recursive function
   to get x*y */
function multiply($x, $y)
{
    if($y)
        return ($x + multiply($x, $y - 1));
    else
        return 0;
}
 
// Driver Code
echo pow(5, 3);
 
// This code is contributed by anuj_67.
?>

Javascript

<script>
 
// A recursive function to get a^b
// Works only if a >= 0 and b >= 0
function pow(a, b)
{
    if (b > 0)
        return multiply(a, pow(a, b - 1));
    else
        return 1;
}
 
// A recursive function to get x*y
function multiply(x, y)
{
    if (y > 0)
        return (x + multiply(x, y - 1));
    else
        return 0;
}
 
// Driver code
document.write(pow(5, 3));
 
// This code is contributed by gauravrajput1
 
</script>

C++

#include <iostream>
using namespace std;
 
//function calculating power
long long pow(int a, int n){
    int ans=1;
      while(n>0){
          // calculate last bit(right most) bit of n
        int last_bit = n&1;
           
          //if last bit is 1 then multiply ans and a
          if(last_bit){
            ans = ans*a;
        }
       
      //make a equal to square of a as on every succeeding bit it got squared like a^0, a^1, a^2, a^4, a^8
      a = a*a;
      n = n >> 1;
    }
      return ans;
}
 
//driver code
int main() {
 
    cout<<pow(3,5);
    return 0;
}

C

#include <stdio.h>
 
// function calculating power
long long pow_(int a, int n){
  int ans = 1;
  while(n > 0)
  {
     
    // calculate last bit(right most) bit of n
    int last_bit = n&1;
 
    // if last bit is 1 then multiply ans and a
    if(last_bit){
      ans = ans*a;
    }
 
    //make a equal to square of a as on every succeeding bit it got squared like a^0, a^1, a^2, a^4, a^8
    a = a*a;
    n = n >> 1;
  }
  return ans;
}
 
// driver code
int main()
{
 
  // pow is an inbuilt function so I have used pow_ as a function name
  printf("%lld",pow_(3,5));
  return 0;
}
 
// This code is contributed by akashish_.

Java

// Java program for the above approach
import java.io.*;
import java.util.*;
 
class GFG
{
 
// function calculating power
static int pow(int a, int n){
    int ans = 1;
      while(n > 0)
      {
         
          // calculate last bit(right most) bit of n
        int last_bit = n&1;
            
          //if last bit is 1 then multiply ans and a
          if(last_bit != 0){
            ans = ans*a;
        }
        
      //make a equal to square of a as on every succeeding bit it got squared like a^0, a^1, a^2, a^4, a^8
      a = a*a;
      n = n >> 1;
    }
      return ans;
}
 
// Driver Code
public static void main(String[] args)
{
    System.out.print(pow(3,5));
}
}
 
// This code is contributed by code_hunt.

Python3

# function calculating power
def pow(a, n):
    ans = 1
    while(n > 0):
       
        #  calculate last bit(right most) bit of n
        last_bit = n&1
           
        # if last bit is 1 then multiply ans and a
        if(last_bit):
            ans = ans*a
       
        # make a equal to square of a as on
        # every succeeding bit it got squared
        # like a^0, a^1, a^2, a^4, a^8
        a = a*a
        n = n >> 1
    return ans
 
# driver code
print(pow(3, 5))
 
# This code is contributed by shinjanpatra

C#

// C# code to implement the approach
using System;
using System.Numerics;
using System.Collections.Generic;
 
public class GFG {
 
// function calculating power
static int pow(int a, int n){
    int ans = 1;
      while(n > 0)
      {
         
          // calculate last bit(right most) bit of n
        int last_bit = n&1;
            
          //if last bit is 1 then multiply ans and a
          if(last_bit != 0){
            ans = ans*a;
        }
        
      //make a equal to square of a as on every succeeding bit it got squared like a^0, a^1, a^2, a^4, a^8
      a = a*a;
      n = n >> 1;
    }
      return ans;
}
 
// Driver Code
public static void Main(string[] args)
{
    Console.Write(pow(3,5));
}
}
 
// This code is contributed by sanjoy_62.

Javascript

<script>
 
// function calculating power
function pow(a, n){
    let ans = 1;
    while(n > 0)
    {
     
        // calculate last bit(right most) bit of n
        let last_bit = n&1;
           
        // if last bit is 1 then multiply ans and a
        if(last_bit)
        {
            ans = ans*a;
        }
       
        // make a equal to square of a as on
        // every succeeding bit it got squared
        // like a^0, a^1, a^2, a^4, a^8
        a = a*a;
        n = n >> 1;
    }
    return ans;
}
 
// driver code
document.write(pow(3,5),"</br>");
 
// This code is contributed by shinjanpatra
 
</script>

PHP

<?php
 
 
// function calculating power
function p_ow($a, $n){
    $ans = 1;
    while($n > 0)
    {
     
        // calculate last bit(right most) bit of n
        $last_bit = $n&1;
           
        // if last bit is 1 then multiply ans and a
        if($last_bit)
        {
            $ans = $ans*$a;
        }
       
        // make a equal to square of a as on
        // every succeeding bit it got squared
        // like a^0, a^1, a^2, a^4, a^8
        $a = $a*$a;
        $n = $n >> 1;
    }
    return $ans;
}
 
// driver code
echo(p_ow(5,3));
   
   
?>

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 *