方法一(使用嵌套循环)
我们可以通过使用重复加法来计算功率。
例如计算 5^6。
1) 前 5 次加 5,我们得到 25。 (5^2)
2) 然后 5 次加 25,我们得到 125。 (5^3)
3) 然后 5 次加 125,我们得到 625 (5^4)
4) 然后 5 次加 625,我们得到 3125 (5^5)
5) 然后 5 次加 3125,我们得到 15625 (5^6)
C++
// C++ code for power function
#include
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
/* 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
= 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
C++
#include
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
/* 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
= 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
输出 :
125
方法二(使用递归)
递归地添加 a 以获得两个数字的乘法。并递归乘法加薪到电力B。
C++
#include
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
/* 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.
蟒蛇3
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
= 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
输出 :
125
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。