mirror of
https://github.com/TheAlgorithms/Ruby
synced 2025-01-13 08:01:03 +01:00
Added solution with log
This commit is contained in:
parent
629de26a20
commit
fbbcdac295
1 changed files with 26 additions and 0 deletions
|
@ -88,3 +88,29 @@ puts is_power_of_two(n)
|
|||
n = 5
|
||||
# Output: false
|
||||
puts is_power_of_two(n)
|
||||
|
||||
#
|
||||
# Approach 3: Using Math library
|
||||
#
|
||||
# Time Complexity: O(1)
|
||||
#
|
||||
def is_power_of_two(n)
|
||||
result_exponent = Math.log(n) / Math.log(2)
|
||||
result_exponent % 1 == 0
|
||||
end
|
||||
|
||||
n = 1
|
||||
# Output: true
|
||||
puts is_power_of_two(n)
|
||||
n = 16
|
||||
# Output: true
|
||||
puts is_power_of_two(n)
|
||||
n = 3
|
||||
# Output: false
|
||||
puts is_power_of_two(n)
|
||||
n = 4
|
||||
# Output: true
|
||||
puts is_power_of_two(n)
|
||||
n = 5
|
||||
# Output: false
|
||||
puts is_power_of_two(n)
|
Loading…
Reference in a new issue