Created
April 4, 2016 03:23
-
-
Save mindplace/6098697725c7c0dd6aed9bc615d37a56 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def to_binary(number) | |
returning = [] | |
if number.odd? | |
returning << 1 | |
number -= 1 | |
end | |
while number > 0 | |
i = 1 | |
while 2 ** i <= number | |
if 2 ** (i + 1) > number | |
returning[i] = 1 | |
number -= (2 ** i) | |
break | |
end | |
i += 1 | |
end | |
end | |
returning.map{|num| num.nil? ? "0" : "#{num}"}.reverse.join.to_i | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment