Consider a sequence of numbers composed of only digits 4 and 7 such as 4, 7, 44, 47, 74,...... 44744,.. etc. Given a number N, we need to find the value of the N-th element in the sequence.
Example 1:
Input:
N = 3
Output:
44
Explanation:
Value of the first term = 4
Value of the second term = 7
Value of the third term = 44
Example 2:
Input:
N = 5
Output:
74
Explanation:
Value of the first term = 4
Value of the second term = 7
Value of the third term = 44
Value of the fourth term = 47
Value of the fifth term = 74
Your Task:  
You don't need to read input or print anything. Your task is to complete the function NthTermOfSeries() which takes an integer N as an input parameter and returns the value of the Nth term of the sequence.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 1000

Source: geeksforgeeks | Difficulty: EASY
Tags: Mathematics

Starter code: #User function Template for python3



class Solution:

    def NthTermOfSeries(self, N):

    	#code here 

Solutions:
class Solution:

	def NthTermOfSeries(self, N):
		if N <= 0:
			return 0
		if N % 2 == 1:
			return 10 * self.NthTermOfSeries((N - 1) // 2) + 4
		return 10 * self.NthTermOfSeries((N - 1) // 2) + 7


class Solution:

	def NthTermOfSeries(self, N):
		k = find(N)
		s = pow(2, k) - 1
		p = bin(N - s)
		p = p[2:]
		r1 = ''
		r1 = r1 + '0' * (k - len(p)) + p
		r2 = ''
		for i in range(len(r1)):
			if r1[i] == '0':
				r2 = r2 + '4'
			else:
				r2 = r2 + '7'
		return r2

def find(n):
	i = 1
	s = 0
	while s < n:
		s = s + pow(2, i)
		i = i + 1
	return i - 1


class Solution:

	def NthTermOfSeries(self, N):
		arr = [0] * (N + 1)
		arr[1] = 4
		arr[2] = 7
		for i in range(3, N + 1):
			if i % 2 != 0:
				arr[i] = arr[i // 2] * 10 + 4
			else:
				arr[i] = arr[i // 2 - 1] * 10 + 7
		return arr[N]


class Solution:

	def NthTermOfSeries(self, N):
		i = 1
		while not (N <= 2 * (2 ** i - 1) and N > 2 * (2 ** (i - 1) - 1)):
			i += 1
		(a, s, N) = (1, 0, N - 2 ** i + 1)
		for j in range(1, i + 1):
			b = 4
			if N > 0:
				b = 4 if N % 2 == 0 else 7
				N //= 2
			s += a * b
			a *= 10
		return s


class Solution:

	def NthTermOfSeries(self, N):
		m = {1: '4', 0: '7'}
		t = ''
		while N != 0:
			t += m[N % 2]
			N = (N - 1) // 2
		return t[::-1]


class Solution:

	def NthTermOfSeries(self, N):
		arr = [0] * (N + 1)
		arr[1] = 4
		arr[2] = 7
		if N == 1:
			return 4
		elif N == 2:
			return 7
		for i in range(3, N + 1):
			if i % 2 != 0:
				arr[i] = 4 + 10 * arr[i // 2]
			else:
				arr[i] = 7 + 10 * arr[i // 2 - 1]
		return arr[N]
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		ans = ob.NthTermOfSeries(N)
		print(ans)


class Solution:

	def NthTermOfSeries(self, N):
		if N <= 0:
			return 0
		if N % 2 == 1:
			return 10 * self.NthTermOfSeries((N - 1) // 2) + 4
		return 10 * self.NthTermOfSeries((N - 1) // 2) + 7
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		ans = ob.NthTermOfSeries(N)
		print(ans)


class Solution:

	def NthTermOfSeries(self, N):

		def recur(count, List, l, n):
			L = []
			for x in List:
				for y in l:
					count += 1
					if count == n:
						return x + y
					L.append(x + y)
			return recur(count, List, L, n)
		return recur(0, ['4', '7'], [''], N)


class Solution:

	def NthTermOfSeries(self, N):
		power = 0
		cur = 2
		ans = 0
		arr = [4, 7]
		N -= 1
		while N >= 0:
			r = N % cur // (cur // 2)
			ans += arr[r] * 10 ** power
			N -= cur
			cur *= 2
			power += 1
		return ans


class Solution:

	def NthTermOfSeries(self, n):
		if n <= 0:
			return 0
		if n & 1:
			return 10 * self.NthTermOfSeries(int((n - 1) / 2)) + 4
		return 10 * self.NthTermOfSeries(int((n - 1) / 2)) + 7


class Solution:

	def NthTermOfSeries(self, n):
		arr = [4, 7]
		for i in range(2, n):
			if i % 2 == 0:
				arr.append(arr[i // 2 - 1] * 10 + 4)
			else:
				arr.append(arr[i // 2 - 1] * 10 + 7)
		return arr[n - 1]