- Check
- 1! + 4! + 5! = 145
Check Strong Number in C#
What you’ll learn
- Strong number definition via digit factorial sums.
- Fast digit factorial lookup for
0..9. - Two C# programs: single-value check and range listing.
- Live preview, edge cases, and complexity using digit count.
Overview
A strong number equals the sum of factorials of its digits. Example: 145 = 1! + 4! + 5! = 1 + 24 + 120 = 145. Classic values are 1, 2, 145, 40585.
Two C# programs
Check one value and list strong numbers from 1 to 200.
Live preview
See factorial-expression sum and verdict instantly.
Simple optimization
Precompute factorials for digits 0 to 9 once.
Prerequisites
Digit extraction, factorial basics, and loop traversal of numbers.
- Use
% 10and/ 10to process digits. - Precompute factorial values for digits 0 through 9.
Understanding the concept of strong numbers
If the sum of factorials of all decimal digits equals the number itself, the number is strong. Also called a digital factorial number.
For 145: 1! = 1, 4! = 24, 5! = 120, and 1 + 24 + 120 = 145.
Math note
Let di be each digit. A strong number satisfies n = ∑ di! over all digits.
145 → 1! + 4! + 5! = 145.
Quick examples
- Sum
- 1!+2!+3! = 9
- Reason
- 2! = 2
Live preview
Type a positive integer to see the factorial sum of its digits and the final verdict.
Algorithm
Goal: decide if integer n is a strong number.
Prepare factorial lookup
Store factorials for digits 0..9 once.
Extract digits
Use modulo and division to process each digit.
Compare sums
If factorial-digit sum equals the original, it is strong.
📜 Pseudocode
fact[0..9] = {1,1,2,6,24,120,720,5040,40320,362880}
function isStrong(n):
sum = 0
x = n
while x > 0:
digit = x mod 10
sum += fact[digit]
x = floor(x / 10)
return sum == n Check one number
Reference program: tests whether 145 is a strong number using a digit-factorial lookup table.
using System;
class Program
{
static bool IsStrongNumber(int num)
{
int[] fact = { 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880 };
int original = num;
int sum = 0;
while (num > 0)
{
int digit = num % 10;
sum += fact[digit];
num /= 10;
}
return sum == original;
}
static void Main()
{
int number = 145;
if (IsStrongNumber(number))
Console.WriteLine($"{number} is a Strong Number.");
else
Console.WriteLine($"{number} is not a Strong Number.");
}
} Strong numbers in range 1 to 200
Reuses IsStrongNumber inside a range loop — matches the reference range output.
using System;
class Program
{
static bool IsStrongNumber(int num)
{
int[] fact = { 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880 };
int original = num;
int sum = 0;
while (num > 0)
{
int digit = num % 10;
sum += fact[digit];
num /= 10;
}
return sum == original;
}
static void Main()
{
Console.WriteLine("Strong Numbers in the Range 1 to 200:");
for (int i = 1; i <= 200; i++)
{
if (IsStrongNumber(i))
Console.Write($"{i} ");
}
Console.WriteLine();
}
} Optimization and alternatives
Lookup table. Precompute 0! through 9! once — each digit contribution is O(1).
Recursive factorial. The old reference used recursion per digit; the lookup table is faster and avoids stack depth.
Short-circuit. Optionally stop when the running sum already exceeds the original number.
❓ FAQ
🔄 Input / output examples
Input n | Result | Factorial sum |
|---|---|---|
145 | Strong | 1!+4!+5! = 145 |
123 | Not strong | 1!+2!+3! = 9 |
2 | Strong | 2! = 2 |
0 | Not strong | 0! = 1 ≠ 0 |
Edge cases and pitfalls
Common issues are handling zero and recomputing factorial repeatedly.
n = 0Usually not strong
Because 0! = 1, the factorial sum does not equal 0 in this variant.
Avoid repeated factorial work
Use the digit-factorial lookup table instead of recalculating each time.
Use long if needed
For very large inputs, accumulate in long to stay safe.
⏱️ Time and space complexity
| Task | Time | Extra space |
|---|---|---|
| Single strong check | O(d) where d is number of digits | O(1) |
| Range 1..U scan | about O(U log U) | O(1) |
Summary
- Definition: strong means sum of
digit!equals the number. - Code: use
fact[10]and digit extraction with% 10. - Range 1..200:
1, 2, 145.
Classic strong numbers in base-10 are 1, 2, 145, and 40585. They are also called digital factorial numbers.
9 people found this page helpful
