Home » Python » Python programs

# Check whether a number is a power of another number or not in Python

Here, we will learn **how to check if a number is a power of another number or not in Python programming language?**

Submitted by Bipin Kumar, on November 21, 2019

To solve this problem simply, we will use the **log() function** from the **math module**. The math module provides us various mathematical operations and here we will use the **log() function** from this module. In Python working of **log() function**, is the same as log work in mathematics. Here, the user will provide us two positive values a and b and we have to **check whether a number is a power of another number or not in Python**. The idea is simple to find the log of a base b and takes the integer part of it and assigns it to a variable s. After this just check if s to the power of b is equal to a then a is the **power of another number b**. Before going to solve this, we will see the algorithm to solve this problem and try to understand it.

**Algorithm to solve this problem:**

- Initially, we will import the math module in the program.
- Takes the positive value of a and b from the user.
- Find the log of a base b and assign its integer part to variable s.
- Also, find the b to the power s and assign it to another variable p.
- Check if p is equal to a then a is a power of another number b and print a is the power of another number b.

Now, we will write the Python program by the implementation of the above algorithm.

**Program:**

# importing the module import math # input the numbers a,b=map(int,input('Enter two values: ').split()) s=math.log(a,b) p=round(s) if (b**p)==a: print('{} is the power of another number {}.'.format(a,b)) else: print('{} is not the power of another number {}.'.format(a,b))

**Output**

RUN 1: Enter two values: 1228 2 1228 is the power of another number 2. RUN 2: Enter two values: 15625 50 15625 is not the power of another number 50.

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions

**Ad:**
Are you a blogger? Join our Blogging forum.