-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path018.Factorials_Less_than_or_Equal_to_n.cpp
50 lines (47 loc) · 1.41 KB
/
018.Factorials_Less_than_or_Equal_to_n.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//Factorials Less than or Equal to n - gfg(easy) (acc : 48%)
//https://www.geeksforgeeks.org/problems/find-all-factorial-numbers-less-than-or-equal-to-n3548/0?problemType=functional&difficulty%255B%255D=-1&page=1&query=problemTypefunctionaldifficulty%255B%255D-1page1
/*
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.
*/
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
// User function Template for C++
class Solution {
public:
long long cnt = 1;
void fun(long long n, vector <long long>& ans, long long i) {
if (i * cnt <= n) {
i *= cnt;
cnt++;
ans.push_back(i);
fun(n, ans, i);
}
else return;
}
vector<long long> factorialNumbers(long long n) {
vector <long long> ans;
fun(n, ans, 1);
return ans;
}
};
//{ Driver Code Starts.
int main() {
int t;
cin >> t;
while (t--) {
long long N;
cin >> N;
Solution ob;
vector<long long> ans = ob.factorialNumbers(N);
for (auto num : ans) {
cout << num << " ";
}
cout << endl;
cout << "~" << endl;
}
return 0;
}
// } Driver Code Ends