Last active
August 29, 2015 14:07
-
-
Save danvonk/d6d686da92af13c4ffd6 to your computer and use it in GitHub Desktop.
binary
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
#!/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