Check if a number is power of another number
In this article, we will discuss the problem of determining whether a given number is a power of another number. We will explore the problem statement, provide an explanation with suitable examples, present an algorithm, pseudocode, and analyze the time complexity of the code.
Problem Introduction
The problem is to check whether a given number N is a power of another number M. In other words, we need to determine if there exists an integer K such that M^K = N. We want to design an algorithm that can solve this problem efficiently for various test cases.
Problem Statement
Given two numbers, M and N, our task is to check if N is a power of M.
For example:
- If M is 3 and N is 27, then the output should be "27 is power of 3."
- If M is 10 and N is 1000, then the output should be "1000 is not power of 10."
- If M is 9 and N is 180, then the output should be "180 is not power of 9."
Algorithm
To solve this problem, we can use a simple algorithm based on the concept of exponentiation. We start with an auxiliary variable initialized to M. We continuously multiply the auxiliary variable by M until it becomes greater than or equal to N. If at any point the auxiliary variable is equal to N, then N is a power of M. Otherwise, it is not.
Pseudocode
is_power(number, result):
status = 0
auxiliary = number
for i from 0 to infinity:
if auxiliary equals result:
status = 1
break
auxiliary = auxiliary multiplied by number
if status equals 0:
print number is not a power of result
else:
print number is a power of result
Code Solution
Here given code implementation process.
//C Program
//Check if a number is power of another number
#include <stdio.h>
//Calculate power (or exponent) of a number
void is_power(int number,int result)
{
int status = 0;
int auxiliary = number;
for (int i = 0; auxiliary <= result; ++i)
{
if(auxiliary == result)
{
//When given power is form of result
status=1;
break;
}
//Calculate power
auxiliary *= number;
}
if(status==0)
{
printf("%d Is not power of %d number \n",number,result );
}
else
{
printf("%d Is power of %d number \n",number,result );
}
}
int main()
{
//Test Case
is_power(3,27);
is_power(10,1000);
is_power(9,180);
return 0;
}
Output
3 Is power of 27 number
10 Is power of 1000 number
9 Is not power of 180 number
/*
C++ Program
Check if a number is power of another number
*/
#include<iostream>
using namespace std;
class MyNumber {
public:
//Calculate power (or exponent) of a number
void is_power(int number, int result) {
int status = 0;
int auxiliary = number;
for (int i = 0; auxiliary <= result; ++i) {
if (auxiliary == result) {
//When given power is form of result
status = 1;
break;
}
//Calculate power
auxiliary *= number;
}
if (status == 0) {
cout << number << " Is not power of " << result << " number \n";
} else {
cout << number << " Is power of " << result << " number \n";
}
}
};
int main() {
MyNumber obj;
//Test case
obj.is_power(3, 27);
obj.is_power(10, 1000);
obj.is_power(9, 180);
return 0;
}
Output
3 Is power of 27 number
10 Is power of 1000 number
9 Is not power of 180 number
/*
Java Program
Check if a number is power of another number
*/
public class MyNumber {
//Calculate power (or exponent) of a number
void is_power(int number,int result)
{
int status = 0;
int auxiliary = number;
for (int i = 0; auxiliary <= result; ++i)
{
if(auxiliary == result)
{
//When given power is form of result
status=1;
break;
}
//Calculate power
auxiliary *= number;
}
if(status==0)
{
System.out.print(number+" Is not power of "+result+" number \n");
}
else
{
System.out.print(number+" Is power of "+result+" number \n");
}
}
public static void main(String[] args) {
MyNumber obj = new MyNumber();
//Test case
obj.is_power(3,27);
obj.is_power(10,1000);
obj.is_power(9,180);
}
}
Output
3 Is power of 27 number
10 Is power of 1000 number
9 Is not power of 180 number
/*
C# Program
Check if a number is power of another number
*/
using System;
public class MyNumber {
//Calculate power (or exponent) of a number
void is_power(int number, int result) {
int status = 0;
int auxiliary = number;
for (int i = 0; auxiliary <= result; ++i) {
if (auxiliary == result) {
//When given power is form of result
status = 1;
break;
}
//Calculate power
auxiliary *= number;
}
if (status == 0) {
Console.Write(number + " Is not power of " + result + " number \n");
} else {
Console.Write(number + " Is power of " + result + " number \n");
}
}
public static void Main(String[] args) {
MyNumber obj = new MyNumber();
//Test case
obj.is_power(3, 27);
obj.is_power(10, 1000);
obj.is_power(9, 180);
}
}
Output
3 Is power of 27 number
10 Is power of 1000 number
9 Is not power of 180 number
# Python 3 Program
# Check if a number is power of another number
class MyNumber :
def is_power(self, number, result) :
status = 0
auxiliary = number
i = 0
while (auxiliary <= result) :
if (auxiliary == result) :
#When given power is form of result
status = 1
break
#Calculate power
auxiliary *= number
i += 1
if (status == 0) :
print(number ," Is not power of ", result ," number")
else :
print(number ," Is power of ", result ," number")
def main() :
obj = MyNumber()
obj.is_power(3, 27)
obj.is_power(10, 1000)
obj.is_power(9, 180)
if __name__ == "__main__":
main()
Output
3 Is power of 27 number
10 Is power of 1000 number
9 Is not power of 180 number
# Ruby Program
# Check if a number is power of another number
class MyNumber
def is_power(number, result)
status = 0
auxiliary = number
i = 0
while (auxiliary <= result)
if (auxiliary == result)
#When given power is form of result
status = 1
break
end
#Calculate power
auxiliary *= number
i += 1
end
if (status == 0)
print(number ," Is not power of ", result ," number \n")
else
print(number ," Is power of ", result ," number \n")
end
end
end
def main()
obj = MyNumber.new()
obj.is_power(3, 27)
obj.is_power(10, 1000)
obj.is_power(9, 180)
end
main()
Output
3 Is power of 27 number
10 Is power of 1000 number
9 Is not power of 180 number
/*
Scala Program
Check if a number is power of another number
*/
import scala.util.control.Breaks._
class MyNumber {
def is_power(number: Int, result: Int): Unit = {
var status: Int = 0;
var auxiliary: Int = number;
var i: Int = 0;
breakable {
while (auxiliary <= result) {
if (auxiliary == result) {
//When given power is form of result
status = 1;
break;
}
//Calculate power
auxiliary *= number;
i += 1;
}
}
if (status == 0) {
print(s"$number Is not power of $result number \n");
} else {
print(s"$number Is power of $result number \n");
}
}
}
object Main {
def main(args: Array[String]): Unit = {
var obj: MyNumber = new MyNumber();
obj.is_power(3, 27);
obj.is_power(10, 1000);
obj.is_power(9, 180);
}
}
Output
3 Is power of 27 number
10 Is power of 1000 number
9 Is not power of 180 number
/*
Swift 4 Program
Check if a number is power of another number
*/
class MyNumber {
func is_power(_ number: Int, _ result: Int) {
var status: Int = 0;
var auxiliary: Int = number;
var i: Int = 0;
while (auxiliary <= result) {
if (auxiliary == result) {
//When given power is form of result
status = 1;
break;
}
//Calculate power
auxiliary *= number;
i += 1;
}
if (status == 0) {
print(number ," Is not power of ", result ," number");
} else {
print(number ," Is power of ", result ," number");
}
}
}
func main() {
let obj: MyNumber = MyNumber();
obj.is_power(3, 27);
obj.is_power(10, 1000);
obj.is_power(9, 180);
}
main();
Output
3 Is power of 27 number
10 Is power of 1000 number
9 Is not power of 180 number
<?php
/*
Php Program
Check if a number is power of another number
*/
class MyNumber {
//Calculate power (or exponent) of a number
function is_power($number, $result) {
$status = 0;
$auxiliary = $number;
for ($i = 0; $auxiliary <= $result; ++$i) {
if ($auxiliary == $result) {
//When given power is form of result
$status = 1;
break;
}
//Calculate power
$auxiliary *= $number;
}
if ($status == 0) {
echo($number ." Is not power of ". $result ." number \n");
} else {
echo($number ." Is power of ". $result ." number \n");
}
}
};
function main() {
$obj = new MyNumber();
//Test case
$obj->is_power(3, 27);
$obj->is_power(10, 1000);
$obj->is_power(9, 180);
}
main();
Output
3 Is power of 27 number
10 Is power of 1000 number
9 Is not power of 180 number
/*
Node Js Program
Check if a number is power of another number
*/
class MyNumber {
//Calculate power (or exponent) of a number
is_power(number, result) {
var status = 0;
var auxiliary = number;
for (var i = 0; auxiliary <= result; ++i) {
if (auxiliary == result) {
//When given power is form of result
status = 1;
break;
}
//Calculate power
auxiliary *= number;
}
if (status == 0) {
process.stdout.write(number + " Is not power of " + result + " number \n");
} else {
process.stdout.write(number + " Is power of " + result + " number \n");
}
}
}
function main(args) {
var obj = new MyNumber();
//Test case
obj.is_power(3, 27);
obj.is_power(10, 1000);
obj.is_power(9, 180)
}
main();
Output
3 Is power of 27 number
10 Is power of 1000 number
9 Is not power of 180 number
Output Explanation
In the provided code, we have three test cases:
- Test Case 1: is_power(3, 27)
The auxiliary variable starts with 3. In the first iteration, it becomes 9 (3 * 3). In the second iteration, it becomes 27 (9 * 3). Since the auxiliary is equal to the result (27), the output is "27 is power of 3." - Test Case 2: is_power(10, 1000)
The auxiliary variable starts with 10. In the first iteration, it becomes 100 (10 * 10). In the second iteration, it becomes 1000 (100 * 10). However, the auxiliary is not equal to the result (1000), so the output is "1000 is not power of 10." - Test Case 3: is_power(9, 180)
The auxiliary variable starts with 9. In the first iteration, it becomes 81 (9 * 9). Since 81 is greater than 180, the loop terminates. As the auxiliary is never equal to the result (180), the output is "180 is not power of 9."
Time Complexity
The time complexity of the provided algorithm is logarithmic, or O(log N), where N is the value of the given number. This is because the number of iterations in the for loop depends on the exponent required to obtain the given number. As we continuously multiply the auxiliary variable by the base number, the number of iterations required to reach or surpass the given number is logarithmic in nature.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment