Given a number (n) and no. of digits (m) to represent the number, we have to check if we can represent n using exactly m digits in any base from 2 to 32.
Example 1:
Input: n = 8, m = 2
Output: Yes 
Explanation: Possible in base 3 as 8 in base 3 is 22.  
Example 2:
Input: n = 8, m = 3
Output: No
Explanation: Not possible in any base. 
Your Task:  
You dont need to read input or print anything. Complete the function baseEquiv() which takes n and m as input parameter and returns "Yes" if its possible to represent the number else "No" without quotes..
Expected Time Complexity: O(logN).
Expected Auxiliary Space: O(1)
Constraints:
1 <= n <=10^{9}
1 <= m <=32

Source: geeksforgeeks | Difficulty: MEDIUM
Tags: Bit manipulation

Sorting

Data structures

Mathematics

Starter code: #User function Template for python3

class Solution:

	def baseEquiv(self, n, m):

		# code here



Solutions:
import math

class Solution:

	def baseEquiv(self, n, m):
		for i in range(2, 32 + 1):
			a = math.ceil(math.log(n, i))
			if a == m:
				return 'Yes'
		return 'No'


from math import log, ceil

class Solution:

	def baseEquiv(self, n, m):
		for i in range(2, 33):
			if ceil(log(n, i)) == m:
				return 'Yes'
		return 'No'


class Solution:

	def solve(self, num, base):
		if num == 0:
			return 0
		d = []
		while num:
			d.append(num % base)
			num = num // base
		return len(d)

	def baseEquiv(self, n, m):
		for i in range(2, 33):
			a = self.solve(n, i)
			if a == m:
				return 'Yes'
		return 'No'


from math import log

class Solution:

	def baseEquiv(self, n, m):
		low = 2
		high = 33
		while low < high:
			mid = (low + high) // 2
			temp = int(log(n, mid)) + 1
			if temp == m:
				return 'Yes'
			if temp < m:
				high = mid
			else:
				low = mid + 1
		return 'No'


from math import log2

class Solution:

	def baseEquiv(self, n, m):
		for x in range(2, 33):
			if int(log2(n) / log2(x) + 1) == m:
				return 'Yes'
		return 'No'


class Solution:

	def baseEquiv(self, n, m):
		for i in range(2, 32 + 1):
			temp = n
			count = 0
			while temp != 0:
				count += 1
				temp //= i
			if count == m:
				return 'Yes'
		return 'No'


from math import log

class Solution:

	def baseEquiv(self, n, m):
		(l, r) = (2, 32)
		while l <= r:
			mid = (l + r) // 2
			digit = int(log(n, mid)) + 1
			if digit == m:
				return 'Yes'
			elif digit > m:
				l = mid + 1
			else:
				r = mid - 1
		return 'No'


class Solution:

	def baseEquiv(self, n, m):
		for base in range(2, 33):
			if self.find_basevalue(n, base) == m:
				return 'Yes'
		return 'No'

	def find_basevalue(self, n, base):
		if n < base:
			return 1
		return self.find_basevalue(n // base, base) + 1


class Solution:

	def baseEquiv(self, n, m):
		for i in range(2, 33):
			temp = n
			c = 0
			while temp:
				temp //= i
				c += 1
			if c == m:
				return 'Yes'
		return 'No'


import math

class Solution:

	def baseEquiv(self, n, m):
		a = 'Yes'
		b = 'No'
		x = 0
		for base in range(2, 33):
			if int(math.log(n, base)) + 1 == m:
				return a
		return b


import math

class Solution:

	def baseEquiv(self, n, m):
		left = 2
		right = 32
		while left <= right:
			mid = (left + right) // 2
			digits = math.log(n) // math.log(mid) + 1
			if digits == m:
				return 'Yes'
			elif digits < m:
				right = mid - 1
			else:
				left = mid + 1
		return 'No'


class Solution:

	def baseEquiv(self, n, m):
		if n <= 32 and m == 1:
			return 'Yes'
		for i in range(32):
			cnt = 0
			x = 1
			while n > x:
				x *= i
				cnt += 1
				if cnt > m:
					break
			if cnt == m:
				return 'Yes'
		return 'No'


class Solution:

	def baseEquiv(self, n, m):

		def getRep(num, base):
			count = 0
			while num:
				num //= base
				count += 1
			return count
		l = 2
		r = 32
		while l <= r:
			b = (l + r) // 2
			rep = getRep(n, b)
			if rep == m:
				return 'Yes'
			elif rep < m:
				r = b - 1
			else:
				l = b + 1
		return 'No'


class Solution:

	def baseEquiv(self, n, m):
		for i in range(2, 33):
			if pow(i, m - 1) > n:
				continue
			if pow(i, m) > n:
				return 'Yes'
		return 'No'


import math

class Solution:

	def baseEquiv(self, n, m):
		for i in range(2, 33):
			if math.floor(math.log(n, i)) + 1 == m:
				return 'Yes'
		return 'No'


class Solution:

	def cntbit(self, n, base):
		cnt = 0
		while n > 0:
			cnt += 1
			n = n // base
		return cnt

	def baseEquiv(self, n, m):
		i = 2
		j = 32
		while i <= j:
			mid = (i + j) // 2
			bcnt = self.cntbit(n, mid)
			if bcnt > m:
				i = mid + 1
			elif bcnt < m:
				j = mid - 1
			else:
				return 'Yes'
		return 'No'


import math

class Solution:

	def baseEquiv(self, n, m):
		for base in range(2, 37):
			dig = math.floor(math.log(n) / math.log(base)) + 1
			if dig == m:
				return 'Yes'
		return 'No'


def reVal(num):
	if num >= 0 and num <= 9:
		return chr(num + ord('0'))
	else:
		return chr(num - 10 + ord('A'))

class Solution:

	def noofbits(self, inputNum, base):
		index = 0
		res = ''
		count = 0
		while inputNum > 0:
			res += reVal(inputNum % base)
			count += 1
			inputNum = int(inputNum / base)
		res = res[::-1]
		return count

	def baseEquiv(self, n, m):
		low = 2
		high = 32
		while low <= high:
			mid = (low + high) // 2
			p = self.noofbits(n, mid)
			if p == m:
				return 'Yes'
			elif p > m:
				low = mid + 1
			else:
				high = mid - 1
		return 'No'


class Solution:

	def baseEquiv(self, n, m):

		def check(n, i):
			s = []
			while n >= i:
				s.append(n % i)
				n = n // i
			s.append(n)
			return len(s)
		if m == 1:
			if n <= 32:
				return 'Yes'
			else:
				return 'No'
		for i in range(2, 33):
			if check(n, i) == m:
				return 'Yes'
		else:
			return 'No'


import math

class Solution:

	def baseEquiv(self, n, m):
		(low, high) = (2, 32)
		while low <= high:
			mid = (low + high) // 2
			if int(math.log(n) / math.log(mid)) + 1 == m:
				return 'Yes'
			elif int(math.log(n) / math.log(mid)) + 1 > m:
				low = mid + 1
			else:
				high = mid - 1
		return 'No'


import math

class Solution:

	def convert(self, n, b):
		ans = ''
		while n > 0:
			ans += str(n % b)
			n = n // b
		return int(ans)

	def baseEquiv(self, n, m):
		for i in range(2, 33):
			ans = math.log(n) // math.log(i) + 1
			if ans == m:
				return 'Yes'
		return 'No'


from math import log, floor

class Solution:

	def baseEquiv(self, n, m):

		def no_of_dig(num, base):
			return floor(log(num) / log(base)) + 1
		(l, h) = (2, 32)
		while l <= h:
			mid = (l + h) // 2
			total = no_of_dig(n, mid)
			if total == m:
				return 'Yes'
			elif total < m:
				h = mid - 1
			else:
				l = mid + 1
		return 'No'


import math

class Solution:

	def baseEquiv(self, n, m):
		for i in range(2, 32 + 1):
			a = math.log10(n)
			b = math.log10(i)
			num = int(a // b)
			num += 1
			if num == m:
				return 'Yes'
		return 'No'


class Solution:

	def baseEquiv(self, n, m):
		for i in range(2, 33):
			num = n
			cnt = 0
			while num:
				cnt += 1
				num //= i
			if cnt == m:
				return 'Yes'
		return 'No'


class Solution:

	def baseEquiv(self, n, m):
		l = []
		if m == 1:
			for i in range(2, 33):
				l.append(i - 1)
		else:
			for i in range(2, 33):
				l.append(i ** (m - 1))
		ans = -1
		low = 0
		high = len(l)
		for i in range(len(l)):
			if l[i] > n:
				ans = i - 1
				break
		if ans == -1:
			ans = len(l) - 1
		f = l[ans]
		ans = ans + 2
		h = ans ** m
		if n >= f and n < h:
			return 'Yes'
		else:
			return 'No'


import math

class Solution:

	def baseEquiv(self, n, m):
		for base in range(2, 33):
			digits = math.log(n) // math.log(base) + 1
			if int(digits) == m:
				return 'Yes'
		return 'No'


import math

class Solution:

	def baseEquiv(self, n, m):
		for i in range(2, 32):
			check = int(math.log(n) / math.log(i) + 1)
			if check == m:
				return 'Yes'
		return 'No'


class Solution:

	def baseEquiv(self, n, m):
		for i in range(2, 33):
			count = 0
			temp = n
			for j in range(m):
				if not temp:
					break
				temp = int(temp / i)
				count += 1
				if temp == 0 and count == m:
					return 'Yes'
		return 'No'


import math

class Solution:

	def baseEquiv(self, n, m):
		if m == 1:
			if n <= 31:
				return 'Yes'
			return 'No'
		x = pow(n, 1 / (m - 1))
		y = pow(n + 1, 1 / m)
		x = math.floor(x)
		y = math.ceil(y)
		if y > x or x < 2 or y > 32:
			return 'No'
		return 'Yes'


from math import log, ceil

class Solution:

	def baseEquiv(self, n, m):
		if self.check(n, m):
			return 'Yes'
		return 'No'

	def checkUtil(self, num, dig, base):
		if dig == 1 and num < base:
			return True
		if dig > 1 and num >= base:
			return self.checkUtil(num // base, dig - 1, base)
		return False

	def check(self, num, dig):
		for base in range(2, 33):
			if self.checkUtil(num, dig, base):
				return True
		return False


class Solution:

	def baseEquiv(self, n, m):

		def chkpos(num, digit, base):
			if digit == 1 and num < base:
				return True
			if digit > 1 and num >= base:
				return chkpos(int(num / base), digit - 1, base)
			return False
		for i in range(2, 33):
			if chkpos(n, m, i):
				return 'Yes'
		return 'No'


import math

class Solution:

	def baseEquiv(self, n, m):
		start = 2
		end = 32
		count = 0
		while start <= end:
			mid = start + (end - start) // 2
			count = math.log(n) // math.log(mid) + 1
			if count == m:
				return 'Yes'
			elif count < m:
				end = mid - 1
			else:
				start = mid + 1
		return 'No'


class Solution:

	def baseEquiv(self, n, m):

		def get(num, b, t):
			if num < b:
				if t + 1 == m:
					return True
				else:
					return False
			elif t >= m:
				return False
			else:
				return get(num // b, b, t + 1)
		for i in range(2, 33):
			if get(n, i, 0):
				return 'Yes'
		return 'No'


class Solution:

	def baseEquiv(self, n, m):
		for i in range(2, 32):
			if i ** (m - 1) > n:
				return 'No'
			elif i ** m - 1 < n:
				pass
			else:
				return 'Yes'
		return 'No'


class Solution:

	def baseEquiv(self, n, m):

		def solve(n, a):
			ans = 0
			while n > 0:
				n //= a
				ans += 1
			return ans
		for i in range(2, 33):
			if solve(n, i) == m:
				return 'Yes'
		return 'No'


class Solution:

	def baseEquiv(self, n, m):
		i = 2
		import math
		while i <= 32:
			if int(math.log(n) // math.log(i)) + 1 == m:
				return 'Yes'
			i += 1
		return 'No'


class Solution:

	def baseEquiv(self, n, m):
		for i in range(2, 33):
			count = m
			val = n
			while val > 0:
				val = val // i
				count -= 1
			if count == 0:
				return 'Yes'
		return 'No'