Boom numbers are numbers consisting only of digits 2 and 3. Given an integer K, display the K-th Boom number.
Example 1:
Input:
K = 5
Output: 32
Explanation:
The Boom series is 2, 3, 22, 23, 32, 33, 222....
and, for K=5, answer = 32
 
Example 2:
Input:
K = 100
Output: 322323
Your Task:  
You don't need to read input or print anything. Your task is to complete the function BoomNumber() which takes an interger K as input parameter and returns the K^{th} boom number in the form of a string.
 
Expected Time Complexity: O( log(K) )
Expected Auxiliary Space: O( log(K) )
Constraints:
1 ≤ K ≤ 10^{12}

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: #User function Template for python3

class Solution:
    def BoomNumber(self, K):
            # code here 

Solutions:
class Solution:

	def BoomNumber(self, K):
		res = ''
		d = {0: '2', 1: '3'}
		n = K + 1
		while n != 1:
			res += d[n % 2]
			n = n // 2
		return res[::-1]


class Solution:

	def BoomNumber(self, K):
		ans = ''
		while K > 0:
			rem = K % 2
			if rem == 1:
				ans += '2'
			else:
				ans += '3'
				K -= 1
			K = K // 2
		return ans[::-1]


import math

class Solution:

	def BoomNumber(self, K):
		x = math.log2(K + 2)
		if x == int(x):
			num_digits = int(x) - 1
		else:
			x = int(x)
			num_digits = x
		new_num = K - 2 ** num_digits + 1
		answer = ''
		for i in range(num_digits):
			if new_num % 2 == 0:
				answer = '2' + answer
			else:
				answer = '3' + answer
			new_num = new_num // 2
		return answer


class Solution:

	def BoomNumber(self, K):
		(x, s) = (2, '')
		while True:
			if K > x:
				K -= x
				x *= 2
			else:
				break
		while x > 1:
			x >>= 1
			if K > x:
				s += '3'
				K -= x
			else:
				s += '2'
		return s


class Solution:

	def BoomNumber(self, K):
		n = ''
		while K > 0:
			rem = K % 2
			if rem == 1:
				n += '2'
			if rem == 0:
				n += '3'
			if K % 2 == 0:
				K = K - 1
			K = K // 2
		return n[::-1]


class Solution:

	def BoomNumber(self, K):
		t = ''
		s = {0: '2', 1: '3'}
		n = K + 1
		while n != 1:
			t += s[n % 2]
			n //= 2
		return t[::-1]