How do you check if a number is a power of another number using recursion in python?
How do I make the Show
SuperStormer 4,7515 gold badges20 silver badges32 bronze badges asked May 14, 2020 at 12:16
1
answered Jul 27, 2020 at 6:46
ShishirShishir 1962 silver badges7 bronze badges 1 No recursion or brute force is required. Just logarithms. When you have
If you want to handle negative numbers as well, you could just calculate x and then check that
answered Jul 15, 2020 at 15:59
Pranav HosangadiPranav Hosangadi 19k5 gold badges41 silver badges65 bronze badges 2 The right way to do it is using recursion since it is asked in the chapter of this exercise. So it will work like this
answered Aug 17, 2020 at 1:25
1
answered Jul 12, 2021 at 2:05
2
It is done by recursion
answered Jul 15, 2020 at 14:27
3 How do you check if a number is a power of another number in Python?1) Initialize pow = x, i = 1 2) while (pow < y) { pow = pow*pow i *= 2 } 3) If pow == y return true; 4) Else construct an array of powers from x^i to x^(i/2) 5) Binary Search for y in array constructed in step 4. If not found, return false.
How do you find the power of a recursive number?Example: Program to Computer Power Using Recursion
This technique can only calculate power if the exponent is a positive integer. To find power of any number, you can use pow() function. result = pow(base, exponent);
How do you check if a number is power of a given number?To check if a given integer x is a power of a given integer y , check if x > 0 && Y % x == 0 : Y is the largest power of y that can fit into an integer datatype. The general idea is that if A is some power of Y , A can be expressed as B/Ya , where a is some integer and A < B .
How do you find the power of a number in Python?How to find the power of a number in Python. import math. print(math. pow(4,2)) Run. Importing math module in Python.. def power(n,e): res=0. for i in range(e): res *= n. return res. print(pow(4,2)) Run. ... . def power(n, e): if e == 0: return 1. elif e == 1: return n. else: return (n*power(n, e-1)). What does it mean when a number is a power of another number?The power (or exponent) of a number says how many times to use the number in a multiplication. It is written as a small number to the right and above the base number. In this example the little "2" says to use 8 two times in a multiplication: 82 = 8 × 8 = 64.
How do you check if a number is a power of 5?If last digit is 5; divide it by 5. If result of division is 1, then number is power of 5.
|