Minimum number of Factorials whose sum is equal to N
Last Updated :
10 Feb, 2022
Given a number N (<1010), the task is to find the minimum number of factorials needed to represent N, as their sum. Also, print those factorials.
Examples:
Input: N = 30
Output: 2
24, 6
Explanation:
Factorials needed to represent 30: 24, 6
Input: N = 150
Output: 3
120, 24, 6
Explanation:
Factorials needed to represent 150: 120 24 6
Approach:
- In order to efficiently find the factorials which are needed to represent N as their sum, we can precompute the factorials till N (N < 1010) and store them in an array, for faster calculations.
- Then using Greedy Algorithm, we can take largest factorials from this array which can be added to represent N.
- Start from largest possible factorial and keep adding factorials while the remaining value is greater than 0.
- Below is the complete algorithm.
- Initialize result as empty
- find the largest factorial that is smaller than N
- Add found factorial to result. Subtract value of found factorial from N
- If N becomes 0, then print result. Else repeat steps 2 and 3 for new value of N
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
ll fact[14];
int size = 1;
void preCompute( int N)
{
fact[0] = 1;
for ( int i = 1; fact[i - 1] <= N; i++) {
fact[i] = (fact[i - 1] * i);
size++;
}
}
void findMin( int N)
{
preCompute(N);
int originalN = N;
vector< int > ans;
for ( int i = size - 1; i >= 0; i--) {
while (N >= fact[i]) {
N -= fact[i];
ans.push_back(fact[i]);
}
}
cout << ans.size() << "\n" ;
for ( int i = 0; i < ans.size(); i++)
cout << ans[i] << " " ;
}
int main()
{
int n = 27;
findMin(n);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int []fact = new int [ 14 ];
static int size = 1 ;
static void preCompute( int N)
{
fact[ 0 ] = 1 ;
for ( int i = 1 ; fact[i - 1 ] <= N; i++) {
fact[i] = (fact[i - 1 ] * i);
size++;
}
}
static void findMin( int N)
{
preCompute(N);
int originalN = N;
Vector<Integer> ans = new Vector<Integer>();
for ( int i = size - 1 ; i >= 0 ; i--) {
while (N >= fact[i]) {
N -= fact[i];
ans.add(fact[i]);
}
}
System.out.print(ans.size()+ "\n" );
for ( int i = 0 ; i < ans.size(); i++)
System.out.print(ans.get(i)+ " " );
}
public static void main(String[] args)
{
int n = 27 ;
findMin(n);
}
}
|
Python3
fact = [ 0 ] * 14
size = 1
def preCompute(N):
global size
fact[ 0 ] = 1
i = 1
while fact[i - 1 ] < = N:
fact[i] = fact[i - 1 ] * i
size + = 1
i + = 1
def findMin(N):
preCompute(N)
originalN = N
ans = []
for i in range (size - 1 , - 1 , - 1 ):
while (N > = fact[i]):
N - = fact[i]
ans.append(fact[i])
print ( len (ans))
for i in ans:
print (i, end = " " )
n = 27
findMin(n)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int []fact = new int [14];
static int size = 1;
static void preCompute( int N)
{
fact[0] = 1;
for ( int i = 1; fact[i - 1] <= N; i++) {
fact[i] = (fact[i - 1] * i);
size++;
}
}
static void findMin( int N)
{
preCompute(N);
int originalN = N;
List< int > ans = new List< int >();
for ( int i = size - 1; i >= 0; i--) {
while (N >= fact[i]) {
N -= fact[i];
ans.Add(fact[i]);
}
}
Console.Write(ans.Count+ "\n" );
for ( int i = 0; i < ans.Count; i++)
Console.Write(ans[i]+ " " );
}
public static void Main(String[] args)
{
int n = 27;
findMin(n);
}
}
|
Javascript
<script>
var fact = Array(14).fill(0);
var size = 1;
function preCompute(N)
{
fact[0] = 1;
for ( var i = 1; fact[i - 1] <= N; i++) {
fact[i] = (fact[i - 1] * i);
size++;
}
}
function findMin(N)
{
preCompute(N);
var originalN = N;
ans = [];
for ( var i = size - 1; i >= 0; i--) {
while (N >= fact[i]) {
N -= fact[i];
ans.push(fact[i]);
}
}
document.write(ans.length + "<br>" );
for ( var i = 0; i < ans.length; i++)
document.write(ans[i] + " " );
}
var n = 27;
findMin(n);
</script>
|
Time Complexity: O(N * size)
Auxiliary Space: O(N * size)
Share your thoughts in the comments
Please Login to comment...