# Check if number is power of 3

Determining whether a given number is a power of 3 or not is an interesting mathematical problem. In this scenario, we want to identify whether a given integer can be expressed as 3 raised to some positive integer exponent. This problem has real-world applications in various fields including computer science, physics, and engineering. It's particularly relevant in cases where computations involving powers of 3 are required, like in algorithms or calculations involving ternary systems.

## Problem Statement and Description

The problem can be framed as follows: Given an integer `number`, we need to determine whether it can be expressed in the form `3^k` where `k` is a positive integer. In other words, we need to identify if `number` is a power of 3.

For instance, if we take the number 81, it can be expressed as `3^4` which is indeed a power of 3. On the other hand, the number 18 cannot be expressed in such a form. Similarly, 27 is `3^3`, making it a power of 3, while 30 is not.

## Idea to Solve the Problem

One way to approach this problem is to leverage the fact that powers of 3 have a distinct property: they are divisors of a very large power of 3. The highest power of 3 that can fit into a 32-bit unsigned integer is 3486784401 (`3^20`). If a number is a power of 3, it must be a divisor of this highest power of 3 without leaving a remainder.

## Pseudocode and Algorithm

``````Function isPowerOfThree(number):
if 3486784401 % number == 0:
print "Number", number, "is a power of 3"
else:
print "Number", number, "is not a power of 3"
``````

## Explanation of Algorithm

1. For each number in the main function, the `isPowerOfThree` function is called.
2. Inside `isPowerOfThree` function, it checks whether the given `number` divides 3486784401 (highest power of 3 that fits in a 32-bit unsigned integer) evenly, i.e., without leaving a remainder.
3. If the condition is satisfied, the function prints that the number is a power of 3; otherwise, it prints that the number is not a power of 3.

## Code Solution

``````// C program for
// Check if number is power of 3
#include <stdio.h>

// Check if that given number is power of 3 or not
void isPowerOfThree(int number)
{
if (3486784401u % number == 0)
{
/*
Highest power of 3 in unsigned integer
3486784401 (3^20)
That is divisible by 3 and remainder is zero
*/
printf(" Number %d is power of 3 \n", number);
}
else
{
printf(" Number %d is not power of 3 \n", number);
}
}
int main(int argc, char const *argv[])
{
// Test Case
isPowerOfThree(81);
isPowerOfThree(18);
isPowerOfThree(27);
isPowerOfThree(30);
isPowerOfThree(-27);
return 0;
}``````

#### input

`````` Number 81 is power of 3
Number 18 is not power of 3
Number 27 is power of 3
Number 30 is not power of 3
Number -27 is not power of 3``````
``````/*
Java Program for
Check if number is power of 3
*/
public class Power
{
// Check whether given number is power of 3 or not
public void isPowerOfThree(long number)
{
if (3486784401L % number == 0)
{
/*
Highest power of 3 in unsigned integer
3486784401 (3^20)
That is divisible by 3 and remainder is zero
*/
System.out.println(" Number " + number + " is power of 3 ");
}
else
{
System.out.println(" Number " + number + " is not power of 3 ");
}
}
public static void main(String[] args)
{
// Test Case
// 3 ^ 0 = 1
}
}``````

#### input

`````` Number 81 is power of 3
Number 18 is not power of 3
Number 27 is power of 3
Number 30 is not power of 3
Number 1 is power of 3
Number -27 is power of 3``````
``````// Include header file
#include <iostream>

using namespace std;
/*
C++ Program for
Check if number is power of 3
*/
class Power
{
public:
// Check whether given number is power of 3 or not
void isPowerOfThree(long number)
{
if (3486784401L % number == 0)
{
/*
Highest power of 3 in unsigned integer
3486784401 (3^20)
That is divisible by 3 and remainder is zero
*/
cout << " Number " << number << " is power of 3 " << endl;
}
else
{
cout << " Number " << number << " is not power of 3 " << endl;
}
}
};
int main()
{
// Test Case
// 3 ^ 0 = 1
return 0;
}``````

#### input

`````` Number 81 is power of 3
Number 18 is not power of 3
Number 27 is power of 3
Number 30 is not power of 3
Number 1 is power of 3
Number -27 is power of 3``````
``````// Include namespace system
using System;
/*
Csharp Program for
Check if number is power of 3
*/
public class Power
{
// Check whether given number is power of 3 or not
public void isPowerOfThree(long number)
{
if (3486784401L % number == 0)
{
/*
Highest power of 3 in unsigned integer
3486784401 (3^20)
That is divisible by 3 and remainder is zero
*/
Console.WriteLine(" Number " + number + " is power of 3 ");
}
else
{
Console.WriteLine(" Number " + number + " is not power of 3 ");
}
}
public static void Main(String[] args)
{
// Test Case
// 3 ^ 0 = 1
}
}``````

#### input

`````` Number 81 is power of 3
Number 18 is not power of 3
Number 27 is power of 3
Number 30 is not power of 3
Number 1 is power of 3
Number -27 is power of 3``````
``````<?php
/*
Php Program for
Check if number is power of 3
*/
class Power
{
// Check whether given number is power of 3 or not
public	function isPowerOfThree(\$number)
{
if (3486784401 % \$number == 0)
{
/*
Highest power of 3 in unsigned integer
3486784401 (3^20)
That is divisible by 3 and remainder is zero
*/
echo " Number ".\$number.
" is power of 3 ".
"\n";
}
else
{
echo " Number ".\$number.
" is not power of 3 ".
"\n";
}
}
}

function main()
{
// Test Case
// 3 ^ 0 = 1
}
main();``````

#### input

`````` Number 81 is power of 3
Number 18 is not power of 3
Number 27 is power of 3
Number 30 is not power of 3
Number 1 is power of 3
Number -27 is power of 3``````
``````/*
Node JS Program for
Check if number is power of 3
*/
class Power
{
// Check whether given number is power of 3 or not
isPowerOfThree(number)
{
if (3486784401 % number == 0)
{
/*
Highest power of 3 in unsigned integer
3486784401 (3^20)
That is divisible by 3 and remainder is zero
*/
console.log(" Number " + number + " is power of 3 ");
}
else
{
console.log(" Number " + number + " is not power of 3 ");
}
}
}

function main()
{
// Test Case
// 3 ^ 0 = 1
}
main();``````

#### input

`````` Number 81 is power of 3
Number 18 is not power of 3
Number 27 is power of 3
Number 30 is not power of 3
Number 1 is power of 3
Number -27 is power of 3``````
``````#  Python 3 Program for
#  Check if number is power of 3
class Power :
#  Check whether given number is power of 3 or not
def isPowerOfThree(self, number) :
if (3486784401 % number == 0) :
#  Highest power of 3 in unsigned integer
#  3486784401 (3^20)
#  That is divisible by 3 and remainder is zero
print(" Number", number ," is power of 3 ")
else :
print(" Number", number ," is not power of 3 ")

def main() :
#  Test Case
#  3 ^ 0 = 1

if __name__ == "__main__": main()``````

#### input

`````` Number 81  is power of 3
Number 18  is not power of 3
Number 27  is power of 3
Number 30  is not power of 3
Number 1  is power of 3
Number -27  is power of 3``````
``````#  Ruby Program for
#  Check if number is power of 3
class Power
#  Check whether given number is power of 3 or not
def isPowerOfThree(number)
if (3486784401 % number == 0)
#  Highest power of 3 in unsigned integer
#  3486784401 (3^20)
#  That is divisible by 3 and remainder is zero
print(" Number ", number ," is power of 3 ", "\n")
else
print(" Number ", number ," is not power of 3 ", "\n")
end

end

end

def main()
#  Test Case
#  3 ^ 0 = 1
end

main()``````

#### input

`````` Number 81 is power of 3
Number 18 is not power of 3
Number 27 is power of 3
Number 30 is not power of 3
Number 1 is power of 3
Number -27 is power of 3
``````
``````/*
Scala Program for
Check if number is power of 3
*/
class Power()
{
// Check whether given number is power of 3 or not
def isPowerOfThree(number: Long): Unit = {
if (3486784401L % number == 0)
{
/*
Highest power of 3 in unsigned integer
3486784401 (3^20)
That is divisible by 3 and remainder is zero
*/
println(" Number " + number + " is power of 3 ");
}
else
{
println(" Number " + number + " is not power of 3 ");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Power = new Power();
// Test Case
// 3 ^ 0 = 1
}
}``````

#### input

`````` Number 81 is power of 3
Number 18 is not power of 3
Number 27 is power of 3
Number 30 is not power of 3
Number 1 is power of 3
Number -27 is power of 3``````
``````/*
Swift 4 Program for
Check if number is power of 3
*/
class Power
{
// Check whether given number is power of 3 or not
func isPowerOfThree(_ number: Int)
{
if (3486784401 % number == 0)
{
/*
Highest power of 3 in unsigned integer
3486784401 (3^20)
That is divisible by 3 and remainder is zero
*/
print(" Number ", number ," is power of 3 ");
}
else
{
print(" Number ", number ," is not power of 3 ");
}
}
}
func main()
{
// Test Case
// 3 ^ 0 = 1
}
main();``````

#### input

`````` Number  81  is power of 3
Number  18  is not power of 3
Number  27  is power of 3
Number  30  is not power of 3
Number  1  is power of 3
Number  -27  is power of 3``````
``````/*
Kotlin Program for
Check if number is power of 3
*/
class Power
{
// Check whether given number is power of 3 or not
fun isPowerOfThree(number: Long): Unit
{

if ((3486784401 % number) == 0L)
{
/*
Highest power of 3 in unsigned integer
3486784401 (3^20)
That is divisible by 3 and remainder is zero
*/
println(" Number " + number + " is power of 3 ");
}
else
{
println(" Number " + number + " is not power of 3 ");
}
}
}
fun main(args: Array < String > ): Unit
{
// Test Case
// 3 ^ 0 = 1
}``````

#### input

`````` Number 81 is power of 3
Number 18 is not power of 3
Number 27 is power of 3
Number 30 is not power of 3
Number 1 is power of 3
Number -27 is power of 3``````

## Time Complexity

The time complexity of this algorithm is O(1), which means it takes a constant amount of time regardless of the input size. This is because the modulo operation (`%`) and the division operation are both constant time operations.

## Another Solution

``````import math

def is_power_of_three(n):
if n <= 0:
return False
else:
return math.log10(n)/math.log10(3) % 1 == 0

# Example usage:
print(is_power_of_three(9))  # True
print(is_power_of_three(27))  # True
print(is_power_of_three(10))  # False``````

## Comment

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.