- Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path0342-power-of-four.rb
49 lines (40 loc) · 892 Bytes
/
0342-power-of-four.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# frozen_string_literal: true
# 342. Power of Four
# Easy
# https://leetcode.com/problems/power-of-four
=begin
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 == 4^x.
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
=end
# @param {Integer} n
# @return {Boolean}
defis_power_of_four(n)
returntrueifn == 1
four=1
whilefour < n
four *= 4
end
four == n ? true : false
end
# **************** #
# TEST #
# **************** #
require"test/unit"
classTest_is_power_of_four < Test::Unit::TestCase
deftest_
assert_equaltrue,is_power_of_four(16)
assert_equalfalse,is_power_of_four(5)
assert_equaltrue,is_power_of_four(1)
end
end