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 == 3x.

Example 1:

Input: n = 27 Output: true Explanation: 27 = 33

Example 2:

Input: n = 0 Output: false Explanation: There is no x where 3x = 0.

Example 3:

Input: n = -1 Output: false Explanation: There is no x where 3x = (-1).

Constraints:

  • -231 <= n <= 231 - 1

Follow up: Could you solve it without loops/recursion?


Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    implicit none

    integer :: n
    logical :: result

    print '(L1)', solve_power_of_three(27, result)
    print '(L1)', solve_power_of_three(0, result)
    print '(L1)', solve_power_of_three(-1, result)

contains

    function solve_power_of_three(n, result) result (result)
        implicit none
        integer, intent(in) :: n
        logical, intent(out) :: result
        integer :: x

        result = .false.
        do while (n > 0)
            x = n / 3
            if (x * 3 == n) then
                result = .true.
                exit
            end if
            n = x
        end do
    end function solve_power_of_three

end program main
Compiled
Executed
Correct
module power_of_three
implicit none
private
public :: is_power_of_three
interface
    function is_power_of_three(n) result(res)
        integer, intent(in) :: n
        logical :: res
    end function is_power_of_three
end interface
contains
function is_power_of_three(n) result(res)
    integer, intent(in) :: n
    logical :: res
    integer :: x
    res = .false.
    if (n == 0) return
    x = 1
    do while (x <= n)
        if (n == 3*x) then
            res = .true.
            exit
        end if
        x = x * 3
    end do
end function is_power_of_three
end module power_of_three

program power_of_three_test
use power_of_three, only : is_power_of_three
implicit none
integer :: n
logical :: res

n = 27
res = is_power_of_three(n)
write (*,*) "n = ", n, "is power of three: ", res

n = 0
res = is_power_of_three(n)
write (*,*) "n = ", n, "is power of three: ", res

n = -1
res = is_power_of_three(n)
write (*,*) "n = ", n, "is power of three: ", res

n = 9
res = is_power_of_three(n)
write (*,*) "n = ", n, "is power of three: ", res

n = 27
res = is_power_of_three(n)
write (*,*) "n = ", n, "is power of three: ", res

n = 81
res = is_power_of_three(n)
write (*,*) "n = ", n, "is power of three: ", res

n = 243
res = is_power_of_three(n)
write (*,*) "n = ", n, "is power of three: ", res

n = 729
res = is_power_of_three(n)
write (*,*) "n = ", n, "is power of three: ", res
end program power_of_three_test
🌐 Data from online sources
def is_power_of_three(n: int) -> bool:
    if n < 1:
        return False

    while n % 3 == 0:
        n //= 3

    return n == 1

The solution checks if the given number, n, is a power of three as follows:

  1. If n is less than 1, return false because powers of 3 are positive integers.
  2. Use a while loop, and keep dividing n by 3 while n is divisible by 3 without a remainder.
  3. After the loop, check if n is equal to 1. If n is 1, it means the original n was a power of 3; otherwise, it was not a power of 3. Return the result accordingly.
🌐 Data from online sources
bool isPowerOfThree(int n) {
    if (n < 1) {
        return false;
    }

    while (n % 3 == 0) {
        n /= 3;
    }

    return n == 1;
}

The solution checks if the given number, n, is a power of three as follows:

  1. If n is less than 1, return false because powers of 3 are positive integers.
  2. Use a while loop, and keep dividing n by 3 while n is divisible by 3 without a remainder.
  3. After the loop, check if n is equal to 1. If n is 1, it means the original n was a power of 3; otherwise, it was not a power of 3. Return the result accordingly.