Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to check a number can be written as a sum of distinct factorial numbers or not in Python
Suppose we have a positive number n, we have to check whether n can be written as the sum of unique positive factorial numbers or not.
So, if the input is like n = 144, then the output will be True, as 4! + 5! = 24 + 120 = 144
Algorithm
To solve this, we will follow these steps −
Generate all factorial numbers up to n
Use a greedy approach: start from the largest factorial and subtract it if possible
Continue until we either reach 0 (success) or can't subtract any more factorials
Step-by-Step Implementation
Generating Factorial Numbers
First, we generate all factorial numbers that are less than or equal to n ?
def generate_factorials(n):
factorials = []
fact = 1
x = 1
while fact <= n:
factorials.append(fact)
x += 1
fact = fact * x
return factorials
# Test with n = 144
factorials = generate_factorials(144)
print("Factorials up to 144:", factorials)
Factorials up to 144: [1, 2, 6, 24, 120]
Complete Solution
Now we implement the complete solution using a greedy approach ?
def can_be_sum_of_factorials(n):
# Generate factorial numbers up to n
fact = 1
factorials = []
x = 1
while fact <= n:
factorials.append(fact)
x += 1
fact = fact * x
# Use greedy approach: start from largest factorial
for i in range(len(factorials)-1, -1, -1):
if n >= factorials[i]:
n -= factorials[i]
return n == 0
# Test examples
test_cases = [144, 26, 10, 150]
for num in test_cases:
result = can_be_sum_of_factorials(num)
print(f"Can {num} be written as sum of distinct factorials? {result}")
Can 144 be written as sum of distinct factorials? True Can 26 be written as sum of distinct factorials? True Can 10 be written as sum of distinct factorials? False Can 150 be written as sum of distinct factorials? True
How It Works
Let's trace through the example with n = 144 ?
def trace_solution(n):
print(f"Finding factorial sum for n = {n}")
# Generate factorials
fact = 1
factorials = []
x = 1
while fact <= n:
factorials.append(fact)
x += 1
fact = fact * x
print(f"Available factorials: {factorials}")
original_n = n
used_factorials = []
# Greedy selection
for i in range(len(factorials)-1, -1, -1):
if n >= factorials[i]:
print(f"Using factorial {factorials[i]}, remaining: {n - factorials[i]}")
used_factorials.append(factorials[i])
n -= factorials[i]
print(f"Used factorials: {used_factorials}")
print(f"Sum: {' + '.join(map(str, used_factorials))} = {sum(used_factorials)}")
print(f"Result: {n == 0}")
return n == 0
trace_solution(144)
Finding factorial sum for n = 144 Available factorials: [1, 2, 6, 24, 120] Using factorial 120, remaining: 24 Using factorial 24, remaining: 0 Used factorials: [120, 24] Sum: 120 + 24 = 144 Result: True
Key Points
The greedy approach works because factorials grow very quickly
We always try to use the largest possible factorial first
Each factorial can be used at most once (distinct factorials)
The algorithm has O(k) time complexity where k is the number of factorials ? n
Conclusion
This greedy algorithm efficiently determines if a number can be expressed as a sum of distinct factorials. The key insight is that using the largest possible factorial at each step leads to the optimal solution due to the rapid growth of factorial values.
