Skip to content

Instantly share code, notes, and snippets.

@danvonk
Last active August 29, 2015 14:07
Show Gist options
  • Save danvonk/d6d686da92af13c4ffd6 to your computer and use it in GitHub Desktop.
Save danvonk/d6d686da92af13c4ffd6 to your computer and use it in GitHub Desktop.
binary
#!/usr/bin/env ruby
def getBinary(dec)
quot = 0
binaryrep = []
#do the first one manually to populate q
quotmod = dec.divmod(2) # [69,0]
q = quotmod[0]
binaryrep.push(quotmod[1])
while q != 0
quotmod = q.divmod(2)
q = quotmod[0]
binaryrep.push(quotmod[1])
end
puts binaryrep.reverse.join
end
#get input i.e. $...binary.rb 10
getBinary ARGV[0].to_i
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment