- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions

# 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

To solve this, we will follow these steps −

fact := 1

res := a new list

x := 2

while fact <= n, do

insert fact at the end of res

fact := fact * x

x := x + 1

for i in range size of res -1 to 0, decrease by 1, do

if n >= res[i], then

n := n - res[i]

return true when n is same as 0

Let us see the following implementation to get better understanding −

## Example

class Solution: def solve(self, n): fact = 1 res = [] x = 2 while fact <= n: res.append(fact) fact = fact * x x += 1 for i in range(len(res)-1,-1,-1): if n>=res[i]: n-=res[i] return n==0 ob = Solution() print(ob.solve(144))

## Input

144

## Output

True

- Related Articles
- Program to check n can be shown as sum of k or not in Python
- Check if a number can be written as sum of three consecutive integers in C++
- Program to check n can be represented as sum of k primes or not in Python
- C++ Program to Check Whether a Number can be Expressed as Sum of Two Prime Numbers
- Java Program to Check Whether a Number can be Expressed as Sum of Two Prime Numbers
- Swift program to check whether a number can be expressed as sum of two prime numbers
- Haskell Program to Check Whether a Number can be Expressed as Sum of Two Prime Numbers
- Check if a prime number can be expressed as sum of two Prime Numbers in Python
- Check if a number can be expressed as a sum of consecutive numbers in C++
- Check if a number can be represented as a sum of 2 triangular numbers in C++
- Check if a number can be expressed as sum two abundant numbers in C++
- Program to check a number is ugly number or not in Python
- Program to check a string can be split into three palindromes or not in Python
- Program to check a string can be broken into given list of words or not in python
- Program to check we can find four elements whose sum is same as k or not in Python

Advertisements