Intuition
The process of converting a decimal number to binary is a recursive technique in itself - by dividing the decimal number by 2 (// 2
) until it’s less than or equal to 1 - and recording all the % 2
at the same time.
Note: int()
also take a argument base = X
where X is the base you want conversion in. For eg. want a octal number? use base = 8
. Want a hexadecimal number? base = 16
. Binary? base = 2
. You get it.
Code
Python3
Big O Analysis
-
Runtime
The runtime complexity here is since we are reducing our input number by half every function call.
-
Memory
The memory usage is since we are not using any extra data structure.
— A