Skip to content

Instantly share code, notes, and snippets.

@mindplace
Created April 4, 2016 03:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mindplace/6098697725c7c0dd6aed9bc615d37a56 to your computer and use it in GitHub Desktop.
Save mindplace/6098697725c7c0dd6aed9bc615d37a56 to your computer and use it in GitHub Desktop.
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