Write a program that performs integer exponentiation of a base b to the power of n.
bⁿ = 1 × b × b × … × b where b is repeated n times
for example
2⁵ = 1 × 2 × 2 × 2 × 2 × 2 = 32
For this exercise, you should consider that 0⁰ = 1.
Input will contain several lines each with two integers b and n where
-1000 ≤ b ≤ 1000
0 ≤ n ≤ 30
For each line of input, output should contain a corresponding line with a single integer indicating the value of bⁿ.
Input will be given so that
-1 000 000 000 < bⁿ < 1 000 000 000
2 5
5 2
32
25
power
functionYour program should contain a power
function
that takes two integers and returns an integer.
The first integer argument should be the base and the second the exponent.
Please refer to the information for your chosen language:
int power(int b, int n);
power :: Int -> Int -> Int
def power(b, n):
int power(int b, int n);
public static int Power(int b, int e)
public static int power(int b, int e)
in a public class Power
function power(b, e)
function power (b, e)
def power(b, e)
You should perform the exponentiation as a series of multiplications. For this exercise, you should restrain from using your programming languages' built-in exponentiation functions.
Specifically for this exercise when using Python, JavaScript, Lua or Ruby,
avoid using sys.exit()
, process.exit()
, os.exit()
or exit
,
as your program is appended with some extra assertions in one of the test sets.
power
functiontry next: fibonacci
Copyright © 2020-2022 Rudy Matela
This text is available under the CC BY-SA 4.0 license.
Originally available on cscx.org/power