**By: Saurav**

**2017-11-12 02:22:00 UTC**

Binary Representation

Given a number N >= 0, find its representation in binary.

Example:

if N = 6,

binary form = 110

This is a very simple problem yet it can be simpler using inbuilt functions in ruby if you know about it. I didn't know about it and I learned something cool and I want to share. :)

The algorithm is same as finding binary representation on a whiteboard/paper.

Figure explains the process:

Lets write some tests first:

All our tests pass and our functions look good as well.

But if we are allowed, here is a one liner answer:

Ruby to_s(base) : Returns a string containing the representation of fix radix base (between 2 and 36).

def find_binary_form(input_decimal) return input_decimal.to_s(2) end

Owned & Maintained by Saurav Prakash

If you like what you see, you can help me cover server costs or buy me a cup of coffee though donation :)