Home PHP Interview Programs Strong Number PHP Strong Number Program Definition
What Is a Strong Number? A strong number (also called a digital factorial number) equals the sum of the factorials of its digits .
n is strong ↔ n = sum of d! for each digit d of n
Example: 145
1! + 4! + 5! = 1 + 24 + 120 = 145 → strong
Classic values: 1, 2, 145, 40585 n Digit factorials Result 1451 + 24 + 120 Strong 11! = 1 Strong 101! + 0! = 2 Not strong 405854!+0!+5!+8!+5! Strong
Approach
How to Check a Strong Number in PHP Four clear steps — a digit-factorial lookup makes the check fast and clear.
Precompute 0! through 9! in a small array (digits never need more). Copy $n, then peel digits with $n % 10. Add $fact[$digit] for each digit. Compare the total with the original number. Method Idea Time Best for Lookup + digit loop Add $fact[$d] for each digit O(digits)Interviews, single checks Range scan Reuse the helper for each value O(N log N)Listing strong numbers Trace terms Print each d! contribution O(digits)Debugging / teaching
Pseudocode fact = [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880]
function IsStrong(n):
if n <= 0:
return false // beginner rule: positive only
original = n
total = 0
while n > 0:
digit = n mod 10
total = total + fact[digit]
n = n / 10
return total == original Reference
Cheat Sheet Keep these facts handy while writing the program.
Topic Detail Definition n == sum of digit factorialsLookup 0!..9! = 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880Peel digit $digit = $n % 10, then $n = intdiv($n, 10)Trivial yes 1, 2Famous 145, 40585vs Armstrong Factorials of digits, not powers 1..200 Only 1, 2, 145
Try it
Live Preview Enter a positive integer to see each digit factorial and the strong/not verdict.
Trace
Worked Walkthrough — n = 145 Peel digits right to left, add factorials, then compare.
n = 145
Digit 5: 5! = 120 total = 120
Digit 4: 4! = 24 total = 144
Digit 1: 1! = 1 total = 145
145 == 145 → strong
Counter-example: 10
0! + 1! = 1 + 1 = 2 ≠ 10 → not strong Code
PHP Programs Start with a single-value check using a factorial lookup. Then list strong numbers in a range, and finish with a term-by-term trace.
1. Check one number 2. Range 1 to 200 3. Trace digit ! Example 1 — Check One Number Precompute 0!..9!, sum digit factorials, and compare with $n.
<?php
function isStrongNumber(int $n): bool
{
$fact = [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880];
$original = $n;
$total = 0;
while ($n > 0) {
$digit = $n % 10;
$total += $fact[$digit];
$n = intdiv($n, 10);
}
return $total === $original;
}
$number = 145;
if (isStrongNumber($number)) {
echo $number . " is a Strong Number.\n";
} else {
echo $number . " is not a Strong Number.\n";
} How It Works The $fact array stores every digit factorial once. Save $original because the loop destroys $n. Each step takes $n % 10 and adds $fact[$digit]. For 145: 120 + 24 + 1 = 145, so the function returns true. Never recompute factorial from scratch inside the digit loop. Example 2 — Strong Numbers from 1 to 200 Reuse the helper to print every strong value in a small band.
<?php
function isStrongNumber(int $n): bool
{
$fact = [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880];
$original = $n;
$total = 0;
while ($n > 0) {
$digit = $n % 10;
$total += $fact[$digit];
$n = intdiv($n, 10);
}
return $total === $original;
}
echo "Strong Numbers in the Range 1 to 200:\n";
for ($i = 1; $i <= 200; $i++) {
if (isStrongNumber($i)) {
echo $i . " ";
}
}
echo "\n"; Strong Numbers in the Range 1 to 200:
1 2 145 How It Works Loop $i from 1 to 200 inclusive. Call isStrongNumber($i) for each value. Print matches only: 1, 2, and 145. The next famous strong number, 40585, is far above 200. This is the usual interview follow-up after a single check. Example 3 — Trace Digit Factorials Print each d! term so you can see why a candidate passes or fails.
<?php
$FACT = [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880];
$values = [2, 10, 145, 99];
foreach ($values as $n) {
$terms = [];
$total = 0;
$x = $n;
while ($x > 0) {
$d = $x % 10;
$total += $FACT[$d];
$terms[] = $d . '!=' . $FACT[$d];
$x = intdiv($x, 10);
}
$terms = array_reverse($terms);
$label = ($total === $n) ? 'strong' : 'not strong';
echo $n . ': ' . implode(' + ', $terms)
. ' = ' . $total . ' -> ' . $label . "\n";
} 2: 2!=2 = 2 -> strong
10: 1!=1 + 0!=1 = 2 -> not strong
145: 1!=1 + 4!=24 + 5!=120 = 145 -> strong
99: 9!=362880 + 9!=362880 = 725760 -> not strong How It Works Digits are collected from right to left, then reversed for readable left-to-right order. Each term shows d! and its value from the lookup table. 10 fails because 1 + 1 = 2, not 10.99 fails because 9! alone is already huge.Use this style when explaining the check on a whiteboard. Edge Cases & Pitfalls Check these before calling the solution done.
1 and 2 Trivial strong numbers Both count. Do not special-case them away.
Zero Usually excluded Beginner checks focus on positive $n. Note that 0! = 1.
Lookup Precompute 0!..9! Digits never exceed 9. Recomputing factorial in a nested loop is wasted work.
Large digits 9! grows fast Numbers with many 9s blow past themselves quickly — useful for early exit if the sum exceeds $n.
Analysis
Time and Space Complexity Program Time Extra space Single check O(digits) = O(log n)O(1)Range of size N O(N log N)O(1)Trace terms O(digits)O(digits) for the term list
The factorial table is fixed size (10 entries), so it does not grow with $n.
Remember
Key Takeaways Definition: $n equals the sum of factorials of its digits.
Lookup: store 0!..9! once, then index by digit.
Classic: 145 = 1! + 4! + 5!.
Test set: 1, 2, 145 strong; 10 and 99 not; 40585 strong.
One line: sum each digit’s factorial — if the total equals $n, it is strong.
Frequently Asked Questions What is a strong number? A strong number equals the sum of the factorials of its digits. Example: 145 = 1! + 4! + 5! = 1 + 24 + 120.
Are 1 and 2 strong numbers? Yes. 1! = 1 and 2! = 2, so both equal the sum of their digit factorials.
How do you check a strong number in PHP? Precompute 0! through 9!, peel digits with n % 10 and intdiv(n, 10), add fact[digit], then compare the total with the original number.
Is a strong number the same as an Armstrong number? No. Armstrong numbers use powers of digits. Strong numbers use factorials of digits.
What is the time complexity? Checking one number is O(d) where d is the digit count (O(log n)). A range scan of size N is O(N log N).
CODETOFUN NEWS
Stay Ahead in Tech Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
11 people found this page helpful
Helpful Share Copy link Suggestion