aboutsummaryrefslogtreecommitdiff
path: root/power_of_three.c
blob: dd1465fd1c225d7bbe956b2a8c4d1bb573001098 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// 326. Power of Three
#include "leetcode.h"

/*
 * given an integer 'n', return true if it is a power of three. otherwise,
 * return false. an integer 'n' is a power of three, if there exists an integer
 * 'x' such that 'n == 3^x'
 */

bool isPowerOfThree(int n) { return n > 0 && !(1162261467 % n); }

int main() {
  printf("%d\n", isPowerOfThree(27)); // expect: 1
  printf("%d\n", isPowerOfThree(0));  // expect: 0
  printf("%d\n", isPowerOfThree(-1)); // expect: 0
}