Given an integer n
, return true
if it is a power of four. Otherwise, return false
.
An integer n
is a power of four, if there exists an integer x
such that n == 4x
.
Example 1:
Input: n = 16 Output: true
Example 2:
Input: n = 5 Output: false
Example 3:
Input: n = 1 Output: true
Constraints:
-231 <= n <= 231 - 1
Follow up: Could you solve it without loops/recursion?
program main
implicit none
integer :: n
logical :: result
! Example 1: 16
n = 16
result = is_power_of_four(n)
print '(L1)', result
! Example 2: 5
n = 5
result = is_power_of_four(n)
print '(L1)', result
! Example 3: 1
n = 1
result = is_power_of_four(n)
print '(L1)', result
contains
function is_power_of_four(n) result(result)
implicit none
integer, intent(in) :: n
logical :: result
integer :: x
result = .false.
do while (n > 0)
x = n / 4
if (x * 4 == n) then
result = .true.
exit
end if
n = x
end do
end function is_power_of_four
end program main
temp.f95:37:12: 37 | n = x | 1 Error: Dummy argument ānā with INTENT(IN) in variable definition context (assignment) at (1)
module power_of_four
implicit none
private
public :: is_power_of_four
contains
function is_power_of_four(n) result(is_four)
integer, intent(in) :: n
logical :: is_four
integer :: x
is_four = .false.
if (n < 1) return
x = 4
do while (x <= n)
if (n == x) then
is_four = .true.
exit
end if
x = x * 4
end do
end function is_power_of_four
end module power_of_four
program power_of_four_test
use power_of_four, only : is_power_of_four
implicit none
integer :: n
n = 16
write (*,*) 'n = ', n, 'is power of four: ', is_power_of_four(n)
n = 5
write (*,*) 'n = ', n, 'is power of four: ', is_power_of_four(n)
n = 1
write (*,*) 'n = ', n, 'is power of four: ', is_power_of_four(n)
n = -231
write (*,*) 'n = ', n, 'is power of four: ', is_power_of_four(n)
n = 231 - 1
write (*,*) 'n = ', n, 'is power of four: ', is_power_of_four(n)
end program power_of_four_test
n = 16 is power of four: T n = 5 is power of four: F n = 1 is power of four: F n = -231 is power of four: F n = 230 is power of four: F
def isPowerOfFour(n: int) -> bool:
if n <= 0:
return False
while n % 4 == 0:
n //= 4
return n == 1
The algorithm checks if a given integer n
is a power of 4 or not. If n
is less than or equal to 0, it immediately returns false
. Otherwise, it enters a loop that checks if n
is divisible by 4. If it is, it keeps dividing the number by 4 until it is no longer divisible by 4. After the loop, the algorithm checks whether the final remainder is 1 or not. If the remainder is 1, the number was initially a power of 4, and it returns true
. Otherwise, it returns false
.
bool isPowerOfFour(int n) {
if (n <= 0) return false;
while (n % 4 == 0) {
n /= 4;
}
return n == 1;
}
The algorithm checks if a given integer n
is a power of 4 or not. If n
is less than or equal to 0, it immediately returns false
. Otherwise, it enters a loop that checks if n
is divisible by 4. If it is, it keeps dividing the number by 4 until it is no longer divisible by 4. After the loop, the algorithm checks whether the final remainder is 1 or not. If the remainder is 1, the number was initially a power of 4, and it returns true
. Otherwise, it returns false
.