A number N is called a factorial number if it is the factorial of a positive integer. For example, the first few factorial numbers are 1, 2, 6, 24, 120,
Given a number N, the task is to return the list/vector of the factorial numbers smaller than or equal to N.
Example 1:
Input: N = 3
Output: 1 2
Explanation: The first factorial number is 
1 which is less than equal to N. The second 
number is 2 which is less than equal to N,
but the third factorial number is 6 which 
is greater than N. So we print only 1 and 2.
Example 2:
Input: N = 6
Output: 1 2 6
Explanation: The first three factorial 
numbers are less than equal to N but 
the fourth factorial number 24 is 
greater than N. So we print only first 
three factorial numbers.
Your Task:  
You don't need to read input or print anything. Your task is to complete the function factorialNumbers() which takes an integer N as an input parameter and return the list/vector of the factorial numbers smaller than or equal to N.
Expected Time Complexity: O(K), Where K is the number of factorial numbers.
Expected Auxiliary Space: O(1)
Constraints:
1<=N<=10^{18}

Source: geeksforgeeks | Difficulty: EASY
Tags: Mathematics

Starter code: #User function Template for python3



class Solution:

    def factorialNumbers(self, N):

    	#code here 

Solutions:
class Solution:

	def factorialNumbers(self, N):

		def findFactorials(num, val, result):
			if val > N:
				return result
			result.append(val)
			return findFactorials(num + 1, val * num, result)
		return findFactorials(2, 1, [])


class Solution:

	def factorialNumbers(self, N):
		i = 1
		res = []
		currFac = 1
		while currFac <= N:
			res.append(currFac)
			i = i + 1
			currFac = currFac * i
		return res


class Solution:

	def factorialNumbers(self, N):
		prev = 1
		factorials = []
		factorials.append(prev)
		curr = prev
		i = 2
		while curr <= N:
			factorials.append(i * prev)
			curr = i * prev
			prev = curr
			i += 1
		factorials.pop()
		return factorials


class Solution:

	def factorialNumbers(self, N):
		fac = 1
		x = 2
		ans = []
		while fac <= N:
			ans.append(fac)
			fac *= x
			x += 1
		return ans


class Solution:

	def factorialNumbers(self, N):
		fact_nums = []
		num = 1
		fact = 1
		while fact <= N:
			fact_nums.append(fact)
			num += 1
			fact *= num
		return fact_nums


class Solution:

	def factorialNumbers(self, N):

		def x(l, k, sum, N):
			if sum > N:
				return l
			l.append(sum)
			return x(l, k + 1, sum * (k + 1), N)
		l = []
		return x(l, 1, 1, N)


import math

class Solution:

	def factorialNumbers(self, N):
		fac = 1
		upt = 2
		ans = []
		while fac <= N:
			ans.append(fac)
			fac *= upt
			upt += 1
		return ans


class Solution:

	def factorialNumbers(self, N):
		answer = []
		fact = 1
		x = 2
		while fact <= N:
			answer.append(fact)
			fact *= x
			x += 1
		return answer


def factorialIterative(n):
	result = 1
	for x in range(1, n + 1):
		result *= x
	return result

class Solution:

	def factorialNumbers(self, N):
		answer = []
		i = 1
		while True:
			a = factorialIterative(i)
			if a <= N:
				answer.append(a)
				i += 1
			else:
				break
		return answer


class Solution:

	def factorialNumbers(self, N):
		l = []

		def fact(c, n):
			if n > N:
				return l
			c += 1
			l.append(n)
			return fact(c, n * c)
		return fact(1, 1)


class Solution:

	def factorialNumbers(self, n):
		if n == 1:
			return [1]
		elif n == 2:
			return [1, 2]
		i = 3
		fact = [1, 2]
		while fact[-1] < n:
			fact.append(fact[-1] * i)
			i += 1
		return fact[:-1]


def fact(N):
	if N == 1:
		return 1
	return N * fact(N - 1)

class Solution:

	def factorialNumbers(self, N):
		if N == 1:
			return [1]
		a = []
		a.append(1)
		for i in range(1, N + 1):
			m = a[i - 1] * i
			if m > N:
				a.pop(0)
				return a
			a.append(a[i - 1] * i)
		a.pop(0)
		return a


class Solution:

	def factorialNumbers(self, N):

		def fact(x):
			if x > 0:
				return x * fact(x - 1)
			else:
				return 1
		res = []
		i = 1
		while True:
			a = fact(i)
			if a <= N:
				res.append(a)
				i = i + 1
			else:
				break
		return res


class Solution:

	def factorialNumbers(self, N):
		a = [1]
		i = 2
		while True:
			if a[-1] * i <= N:
				a.append(a[-1] * i)
				i = i + 1
			else:
				break
		return a


class Solution:

	def factorialNumbers(self, N):

		def fact(x):
			if x <= 1:
				return 1
			return x * fact(x - 1)
		i = 1
		l = []
		while i <= N:
			f = fact(i)
			i += 1
			if f <= N:
				l.append(f)
			else:
				break
		return l


class Solution:

	def factorial(self, n):
		fact = 1
		for i in range(1, n + 1):
			fact = fact * i
		return fact

	def factorialNumbers(self, N):
		i = 1
		ans = []
		while True:
			if self.factorial(i) <= N:
				ans.append(self.factorial(i))
				i += 1
			else:
				return ans


import math

class Solution:

	def factorialNumbers(self, N):
		s = 1
		l = []
		for i in range(1, int(math.sqrt(N)) + 2):
			s *= i
			if s > N:
				break
			l.append(s)
		return l


class Solution:

	def factorialNumbers(self, N):
		x = 1
		y = 2
		arr = [1]
		while x * y <= N:
			arr.append(x * y)
			x = x * y
			y += 1
		return arr


class Solution:

	def factorialNumbers(self, N):

		def isfact(n, fact):
			if fact > N:
				return []
			else:
				return [fact] + isfact(n + 1, (n + 1) * fact)
		return isfact(1, 1)


class Solution:

	def factorialNumbers(self, N):
		l = [1]
		for i in range(2, N + 1):
			a = l[-1] * i
			if a <= N:
				l.append(a)
			else:
				break
		return l


class Solution:

	def factorialNumbers(self, N):

		def fact(x):
			if x == 1:
				return 1
			return x * fact(x - 1)
		res = []
		k = 1
		while True:
			f = fact(k)
			if f <= N:
				res.append(f)
			else:
				break
			k += 1
		return res


class Solution:

	def factorialNumbers(self, N):
		res = []

		def fact(x):
			if x == 0:
				return 1
			return x * fact(x - 1)
		n = 1
		while fact(n) <= N:
			res.append(fact(n))
			n += 1
		return res


class Solution:

	def factorial(self, N):
		if N == 0:
			return 1
		fact = N * self.factorial(N - 1)
		return fact

	def factorialNumbers(self, N):
		lst = []
		count = 0
		for i in range(1, N + 1):
			fct = self.factorial(i)
			if fct > N:
				return lst
			lst.append(fct)
		return lst


class Solution:

	def factorialNumbers(self, N):
		arr = []
		factorial = 1
		for i in range(1, N + 1):
			factorial *= i
			if factorial <= N:
				arr.append(factorial)
			else:
				break
		return arr


class Solution:

	def factorialNumbers(self, N):

		def fact(N):
			if N > 1:
				return N * fact(N - 1)
			else:
				return N
		ans = []
		if N == 1:
			return [1]
		i = 1
		x = 1
		while x < N:
			x = fact(i)
			if x <= N:
				ans.append(x)
			i += 1
		return ans


class Solution:

	def factorialNumbers(self, N):
		factorial_list = []
		num = 1
		factorial = 1
		while factorial <= N:
			factorial_list.append(factorial)
			num += 1
			factorial *= num
		return factorial_list


def fun(n):
	d = 1
	for i in range(1, n + 1):
		d *= i
	return d

class Solution:

	def factorialNumbers(self, N):
		d = []
		for i in range(1, N + 1):
			res = fun(i)
			if res > N:
				break
			else:
				if res == N:
					d.append(N)
					break
				d.append(res)
		return d


class Solution:

	def factorialNumbers(self, N):
		arr = [1]
		i = 0
		while True:
			fact = arr[i] * (i + 1)
			if fact <= N:
				arr.append(fact)
			else:
				break
			i += 1
		return arr[1:]


class Solution:

	def factorialNumbers(self, N):

		def factRec(N):
			if N == 1:
				return 1
			return N * factRec(N - 1)
		start = 1
		ans = []
		while True:
			result = factRec(start)
			start += 1
			if result <= N:
				ans.append(result)
			else:
				return ans


class Solution:

	def factorialNumbers(self, N):
		l = []
		i = 1
		z = 2
		while i <= N:
			l.append(i)
			i *= z
			z += 1
		return l


class Solution:

	def factorial(self, N):
		if N == 0 or N == 1:
			return 1
		return N * self.factorial(N - 1)

	def factorialNumbers(self, N):
		l = []
		for i in range(1, N + 1):
			k = self.factorial(i)
			if k > N:
				return l
			l.append(k)
		return l


class Solution:

	def factorialNumbers(self, N):
		f_num = [1]
		i = 1
		while True:
			temp = f_num[i - 1] * (i + 1)
			i += 1
			if temp <= N:
				f_num.append(temp)
			else:
				return f_num


class Solution:

	def factorialNumbers(self, N):
		i = 1
		factorial = 1
		factorial_numbers = []
		while factorial <= N:
			factorial_numbers.append(factorial)
			i += 1
			factorial *= i
		return factorial_numbers


class Solution:

	def factorialNumbers(self, N):

		def getFact(n):
			if n <= 0:
				return 1
			return n * getFact(n - 1)
		ans = []
		start = 1
		i = 1
		while True:
			start = getFact(i)
			if start > N:
				break
			ans.append(start)
			i += 1
		return ans


class Solution:

	def __init__(self):
		self.res = []

	def factorialNumbers(self, N, k=1, temp=1):
		if N <= 1:
			return [1]
		if temp > N:
			return self.res
		else:
			self.res.append(temp)
			self.factorialNumbers(N, k + 1, temp * (k + 1))
		return self.res


class Solution:

	def fact(self, n):
		if n == 1:
			return 1
		return n * self.fact(n - 1)

	def factorialNumbers(self, N):
		ls = []
		for i in range(1, N + 1):
			x = self.fact(i)
			if x <= N:
				ls.append(x)
			else:
				return ls
		return ls


class Solution:

	def factorialNumbers(self, N):

		def fact(n):
			if n == 1 or n == 0:
				return 1
			else:
				return n * fact(n - 1)
		num = 1
		i = 1
		ans = [1]
		while num <= N:
			i += 1
			num = fact(i)
			ans.append(num)
		if num > N:
			ans.pop()
		return ans


class Solution:

	def factorialNumbers(self, N):

		def calculate(n, final, mul):
			if n * mul <= final:
				list_.append(n * mul)
				calculate(n * mul, final, mul + 1)
			else:
				return
		list_ = []
		calculate(1, N, 1)
		return list_


class Solution:

	def factorialNumbers(self, N):
		lst = []
		for i in range(1, N // 2 + 2):
			if self.fact(i) > N:
				return lst
			else:
				lst.append(self.fact(i))
		return lst

	def fact(self, n):
		if n == 0:
			return 1
		else:
			return n * self.fact(n - 1)


class Solution:

	def factorialNumbers(self, N):
		import math
		i = 1
		k = []
		while math.factorial(i) <= N:
			k.append(math.factorial(i))
			i = i + 1
		return k


class Solution:

	def factorialNumbers(self, N):
		factorials = []
		result = 1
		i = 1
		while result <= N:
			factorials.append(result)
			i += 1
			result *= i
		return factorials


class Solution:

	def factorialNumbers(self, n):
		arr = []
		fact = 1
		x = 2
		while fact <= n:
			arr.append(fact)
			fact = fact * x
			x = x + 1
		return arr


class Solution:

	def factorialNumbers(self, N):

		def solve(x, i):
			if x > N:
				return []
			small = solve(x * i, i + 1)
			small.append(x)
			return small
		ans = solve(1, 1)
		ans.pop()
		return ans[::-1]


class Solution:

	def getFact(self, N):
		if N == 1:
			return 1
		return N * self.getFact(N - 1)

	def factorialNumbers(self, N):
		l = []
		for i in range(1, N + 1):
			res = self.getFact(i)
			if res > N:
				break
			l.append(res)
		return l


class Solution:

	def factorial(n: int) -> int:
		if n == 0:
			return 1
		return Solution.factorial(n - 1) * n

	def factorialNumbers(self, n):
		ans = []
		for i in range(1, n + 1):
			fact = Solution.factorial(i)
			if fact <= n:
				ans.append(fact)
			else:
				break
		return ans


class Solution:

	def factorialNumbers(self, n):
		l = []
		p = 1
		for i in range(1, 10 ** 6):
			p = p * i
			if p <= n:
				l.append(p)
			else:
				break
		return l


class Solution:

	def factorialNumbers(self, N):
		a = []

		def fac(x, i):
			if x * i <= N:
				a.append(x * i)
				fac(x * i, i + 1)
		fac(1, 1)
		return a


class Solution:

	def factorialNumbers(self, N):
		fact = 1
		i = 2
		lst = []
		while fact <= N:
			lst.append(fact)
			fact *= i
			i += 1
		return lst


class Solution:

	def factorialNumbers(self, N):
		res = list()
		i = 1
		j = 2
		self.helper(N, res, i, j)
		return res

	def helper(self, N, res, i, j):
		if i > N:
			return ()
		res.append(i)
		return self.helper(N, res, i * j, j + 1)


class Solution:

	def factorialNumbers(self, N):
		ls = []

		def sec(n, f):
			if N < f:
				return
			ls.append(f)
			sec(n + 1, f * n)
		sec(2, 1)
		return ls


class Solution:

	def factorialNumbers(self, N):
		l = []
		s = 1
		i = 1
		while s <= N:
			l.append(s)
			i += 1
			s = s * i
		return l


import math

class Solution:

	def factorialNumbers(self, N):
		if N == 1:
			return [1]
		i = 1
		fact = 1
		a = []
		while fact <= N:
			a.append(fact)
			i += 1
			fact = i * fact
		return a


class Solution:

	def factorialNumbers(self, N):
		ans = []
		fact = 1
		for i in range(1, N + 1):
			fact *= i
			if fact <= N:
				ans.append(fact)
			else:
				break
		return ans


class Solution:

	def factorialNumbers(self, N):
		fact = [1, 1]
		i = 2
		while True:
			fact.append(fact[i - 1] * i)
			if fact[i] > N:
				return fact[1:i]
			i += 1
		return fact


class Solution:

	def factorialNumbers(self, N):

		def fact(n):
			if n == 1:
				return 1
			else:
				fact = 1
				while n != 1:
					fact *= n
					n -= 1
				return fact
		count = []
		temp = 1
		while True:
			number = fact(temp)
			if number <= N:
				count.append(number)
				temp += 1
			else:
				break
		return count


class Solution:

	def factorialNumbers(self, n):
		i = 1
		fact = 1
		res = []
		while fact <= n:
			res.append(fact)
			i = i + 1
			fact = fact * i
		return res


class Solution:

	def factorialNumbers(self, N):
		ans = []
		a = 1
		b = 1
		while a <= N:
			ans.append(a)
			b += 1
			a *= b
		return ans


class Solution:

	def factorialNumbers(self, N):
		ans = []
		fact = 1
		mul = 1
		while fact <= N:
			ans.append(fact)
			mul += 1
			fact *= mul
		return ans


class Solution:

	def get_factorial(self, n, fac, i, res):
		i += 1
		if fac > n:
			return
		res.append(fac)
		return self.get_factorial(n, fac * i, i, res)

	def factorialNumbers(self, N):
		res = []
		self.get_factorial(N, 1, 1, res)
		return res


class Solution:

	def factorialNumbers(self, N):
		t = 1
		arr = []
		tot = 1
		while tot <= N:
			arr.append(tot)
			t += 1
			tot *= t
		return arr


class Solution:

	def factorialNumbers(self, N):
		a = []
		f = 1
		for i in range(1, N + 1):
			f = f * i
			if f <= N:
				a.append(f)
			else:
				return a
		return a


class Solution:

	def factorialNumbers(self, N):
		facts = [1]
		if N == 1:
			return facts
		i = 1
		while facts[-1] < N:
			facts.append(i * facts[i - 1])
			i += 1
		facts.remove(1)
		if facts[-1] > N:
			facts.pop()
		return facts