Skip to content

Instantly share code, notes, and snippets.

@barbchoy
Created October 2, 2017 05:44
Show Gist options
  • Save barbchoy/a2704167dcea8079572a32fd673e99c6 to your computer and use it in GitHub Desktop.
Save barbchoy/a2704167dcea8079572a32fd673e99c6 to your computer and use it in GitHub Desktop.
power-of-2 created by barbchoy - https://repl.it/Br7W/2659
# Write a method that takes in a number and returns true if it is a
# power of 2. Otherwise, return false.
#
# You may want to use the `%` modulo operation. `5 % 2` returns the
# remainder when dividing 5 by 2; therefore, `5 % 2 == 1`. In the case
# of `6 % 2`, since 2 evenly divides 6 with no remainder, `6 % 2 == 0`.
#
# Difficulty: medium.
def is_power_of_two?(num)
result = false
while num%2==0 && num!=0
num=num/2
result =true
end
if num%2!=0
result = false
end
if num==1
result=true
end
return result
end
# These are tests to check that your code is working. After writing
# your solution, they should all print true.
puts("\nTests for #is_power_of_two?")
puts("===============================================")
puts('is_power_of_two?(1) == true: ' + (is_power_of_two?(1) == true).to_s)
puts('is_power_of_two?(16) == true: ' + (is_power_of_two?(16) == true).to_s)
puts('is_power_of_two?(64) == true: ' + (is_power_of_two?(64) == true).to_s)
puts('is_power_of_two?(78) == false: ' + (is_power_of_two?(78) == false).to_s)
puts('is_power_of_two?(0) == false: ' + (is_power_of_two?(0) == false).to_s)
puts("===============================================")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment