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 - 1Follow up: Could you solve it without loops/recursion?
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
temp.f95:13:60:
13 | function solve_power_of_three(n, result) result (result)
| 1
Error: DUMMY attribute conflicts with RESULT attribute in ‘result’ at (1)
temp.f95:14:21:
14 | implicit none
| 1
Error: Unexpected IMPLICIT NONE statement in CONTAINS section at (1)
temp.f95:15:32:
15 | integer, intent(in) :: n
| 1
Error: Unexpected data declaration statement in CONTAINS section at (1)
temp.f95:16:38:
16 | logical, intent(out) :: result
| 1
Error: Unexpected data declaration statement in CONTAINS section at (1)
temp.f95:17:20:
17 | integer :: x
| 1
Error: Unexpected data declaration statement in CONTAINS section at (1)
temp.f95:19:24:
19 | result = .false.
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:20:24:
20 | do while (n > 0)
| 1
Error: Unexpected DO statement in CONTAINS section at (1)
temp.f95:21:21:
21 | x = n / 3
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:22:32:
22 | if (x * 3 == n) then
| 1
Error: Unexpected block IF statement in CONTAINS section at (1)
temp.f95:23:31:
23 | result = .true.
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:24:20:
24 | exit
| 1
Error: EXIT statement at (1) is not within a construct
temp.f95:25:15:
25 | end if
| 1
Error: Expecting END PROGRAM statement at (1)
temp.f95:26:17:
26 | n = x
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:27:11:
27 | end do
| 1
Error: Expecting END PROGRAM statement at (1)
temp.f95:28:7:
28 | end function solve_power_of_three
| 1
Error: Expecting END PROGRAM statement at (1)
temp.f95:7:17:
7 | print '(L1)', solve_power_of_three(27, result)
| 1
Error: Function ‘solve_power_of_three’ at (1) has no IMPLICIT type
temp.f95:8:17:
8 | print '(L1)', solve_power_of_three(0, result)
| 1
Error: Function ‘solve_power_of_three’ at (1) has no IMPLICIT type
temp.f95:9:17:
9 | print '(L1)', solve_power_of_three(-1, result)
| 1
Error: Function ‘solve_power_of_three’ at (1) has no IMPLICIT type
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
temp.f95:12:27:
6 | function is_power_of_three(n) result(res)
| 2
......
12 | function is_power_of_three(n) result(res)
| 1
Error: Procedure ‘is_power_of_three’ at (1) is already defined at (2)
temp.f95:13:28:
13 | integer, intent(in) :: n
| 1
Error: Unexpected data declaration statement in CONTAINS section at (1)
temp.f95:14:18:
14 | logical :: res
| 1
Error: Unexpected data declaration statement in CONTAINS section at (1)
temp.f95:15:16:
15 | integer :: x
| 1
Error: Unexpected data declaration statement in CONTAINS section at (1)
temp.f95:16:17:
16 | res = .false.
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:17:22:
17 | if (n == 0) return
| 1
Error: Unexpected simple IF statement in CONTAINS section at (1)
temp.f95:18:9:
18 | x = 1
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:19:21:
19 | do while (x <= n)
| 1
Error: Unexpected DO statement in CONTAINS section at (1)
temp.f95:20:26:
20 | if (n == 3*x) then
| 1
Error: Unexpected block IF statement in CONTAINS section at (1)
temp.f95:21:24:
21 | res = .true.
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:22:16:
22 | exit
| 1
Error: EXIT statement at (1) is not within a construct
temp.f95:23:11:
23 | end if
| 1
Error: Expecting END MODULE statement at (1)
temp.f95:24:17:
24 | x = x * 3
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:25:7:
25 | end do
| 1
Error: Expecting END MODULE statement at (1)
temp.f95:26:3:
26 | end function is_power_of_three
| 1
Error: Expecting END MODULE statement at (1)
temp.f95:30:5:
30 | use power_of_three, only : is_power_of_three
| 1
Fatal Error: Cannot open module file ‘power_of_three.mod’ for reading at (1): No such file or directory
compilation terminated.
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:
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: