Given a positive integer, write a function to find if it is a power of three or not.

Examples:

Input : 3 Output :Yes Input :6 Output :No

The logic is very simple. Any integer number other than power of 3 which divides highest power of 3 value that integer can hold 3^19 = 1162261467 (Assuming that integers are stored using 32 bits) will give reminder non-zero.

## C++

`// C++ program to check if a number is power ` `// of 3 or not. ` `#include <stdio.h> ` ` ` `// Returns true if n is power of 3, else false ` `bool` `check(` `int` `n) ` `{ ` ` ` `/* The maximum power of 3 value that ` ` ` `integer can hold is 1162261467 ( 3^19 ) .*/` ` ` `return` `1162261467 % n == 0; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 9; ` ` ` `if` `(check(n)) ` ` ` `printf` `(` `"Yes"` `); ` ` ` `else` ` ` `printf` `(` `"No"` `); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to check if a number is power ` `// of 3 or not. ` `public` `class` `Power_3 { ` ` ` ` ` `// Returns true if n is power of 3, else false ` ` ` `static` `boolean` `check(` `int` `n) ` ` ` `{ ` ` ` `/* The maximum power of 3 value that ` ` ` `integer can hold is 1162261467 ( 3^19 ) .*/` ` ` `return` `1162261467` `% n == ` `0` `; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `int` `n = ` `9` `; ` ` ` `if` `(check(n)) ` ` ` `System.out.println(` `"Yes"` `); ` ` ` `else` ` ` `System.out.println(` `"No"` `); ` ` ` `} ` `} ` `// This code is contributed by Sumit Ghosh ` |

*chevron_right*

*filter_none*

## Python

`# Python program to check if a number is power ` `# of 3 or not. ` ` ` `# Returns true if n is power of 3, else false ` `def` `check(n): ` ` ` `""" The maximum power of 3 value that ` ` ` `integer can hold is 1162261467 ( 3^19 ) ."""` ` ` `return` `1162261467` `%` `n ` `=` `=` `0` ` ` `# Driver code ` `n ` `=` `9` `if` `(check(n)): ` ` ` `print` `(` `"Yes"` `) ` `else` `: ` ` ` `print` `(` `"No"` `) ` ` ` `# This code is contributed by Sachin Bisht ` |

*chevron_right*

*filter_none*

## C#

`// C# program to check if a number ` `// is power of 3 or not. ` `using` `System; ` ` ` `public` `class` `GFG { ` ` ` ` ` `// Returns true if n is power ` ` ` `// of 3, else false ` ` ` `static` `bool` `check(` `int` `n) ` ` ` `{ ` ` ` ` ` `/* The maximum power of 3 ` ` ` `value that integer can hold ` ` ` `is 1162261467 ( 3^19 ) .*/` ` ` `return` `1162261467 % n == 0; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 9; ` ` ` `if` `(check(n)) ` ` ` `Console.Write(` `"Yes"` `); ` ` ` `else` ` ` `Console.Write(` `"No"` `); ` ` ` `} ` `} ` ` ` `// This code is contributed by ` `// nitin mittal. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to check if a ` `// number is power of 3 or not. ` ` ` `// Returns true if n is ` `// power of 3, else false ` `function` `check(` `$n` `) ` `{ ` ` ` ` ` `/* The maximum power of 3 value that ` ` ` `integer can hold is 1162261467 ` ` ` `( 3^19 ) . */` ` ` `return` `1162261467 % ` `$n` `== 0; ` `} ` ` ` ` ` `// Driver code ` ` ` `$n` `= 9; ` ` ` `if` `(check(` `$n` `)) ` ` ` `echo` `(` `"Yes"` `); ` ` ` `else` ` ` `echo` `(` `"No"` `); ` ` ` `// This code is contributed by nitin mittal ` `?> ` |

*chevron_right*

*filter_none*

Output:

Yes

* Time Complexity : O(1)*

This article is contributed by **Jebasingh and Riyazath**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Find ways an Integer can be expressed as sum of n-th power of unique natural numbers
- Find power of power under mod of a prime
- Find value of y mod (2 raised to power x)
- Program to find whether a no is power of two
- Find whether a given number is a power of 4 or not
- Find One's Complement of an Integer
- Given two numbers as strings, find if one is a power of other
- Find the super power of a given Number
- Find multiple of x closest to or a ^ b (a raised to power b)
- Find unit digit of x raised to power y
- Find an integer X which is divisor of all except exactly one element in an array
- Find maximum power of a number that divides a factorial
- Discrete logarithm (Find an integer k such that a^k is congruent modulo b)
- Check if given number is a power of d where d is a power of 2
- Compute power of power k times % m