Check Strong Number in JavaScript
What you’ll learn
- What a strong number (digital factorial) means.
- How to compute factorials of digits efficiently using a small lookup table.
- Two complete JavaScript programs: check one number and list strong numbers from 1 to 200.
Overview
A strong number is a positive integer that equals the sum of the factorials of its digits. Example: 145 is strong because 1! + 4! + 5! = 145. This page shows a clean C implementation and a small range listing.
Two programs
Example 1 checks 145. Example 2 prints all strong numbers in 1–200 (output: 1 2 145).
Live preview
Type a number and see the factorial addition (like 1! + 4! + 5!) and the verdict.
Simple optimization
Precompute 0! to 9! once, then each digit factorial is just an array lookup.
Prerequisites
Digit extraction with % and integer division, plus basic functions.
for/whileloops and printing output withconsole.log.- Understanding
n % 10(last digit) andn / 10(remove last digit).
What is a strong number?
A strong number (also called a digital factorial) is a number where:
number = sum of factorials of its digitsFor 145, the digits are 1, 4, 5. Their factorials are 1! = 1, 4! = 24, 5! = 120. Adding them gives 145 again.
1451! + 4! + 5! = 1 + 24 + 120 = 145 — so 145 is a strong number.
Live preview
Type a positive integer and see the factorial sum of its digits. (This demo caps inputs for speed.)
Algorithm
Goal: compute the sum of digit! for each digit of n and compare to n.
Precompute factorials of digits
Store 0! through 9! in an array fact[10]. This is tiny and avoids calling factorial repeatedly.
Extract digits and add
While a working copy x is greater than 0: take digit = x % 10, add fact[digit] to sum, then do x /= 10.
Compare
If sum == n, the number is strong; otherwise it is not.
📜 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 ← sum + fact[digit]
x ← floor(x / 10)
return sum = nCheck a single number
Uses a digit-factorial lookup table instead of recursion. Sample input is 145.
function isStrongNumber(n) {
const fact = [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880];
const original = n;
let sum = 0;
while (n > 0) {
const digit = n % 10;
sum += fact[digit];
n = Math.floor(n / 10);
}
return sum === original;
}
const number = 145;
if (isStrongNumber(number)) {
console.log(number + " is a Strong Number.");
} else {
console.log(number + " is not a Strong Number.");
}Strong numbers from 1 to 200
Loops through the range and prints values that satisfy the same check. Output is 1 2 145.
function isStrongNumber(n) {
const fact = [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880];
const original = n;
let sum = 0;
while (n > 0) {
const digit = n % 10;
sum += fact[digit];
n = Math.floor(n / 10);
}
return sum === original;
}
const values = [];
for (let i = 1; i <= 200; i++) {
if (isStrongNumber(i)) {
values.push(String(i));
}
}
console.log("Strong Numbers in the Range 1 to 200:");
console.log(values.join(" "));Optimization
Lookup table. The biggest win is precomputing 0! through 9!. After that, each digit contributes in O(1) time.
Short-circuit. If the running sum already exceeds the original number, you can stop early (optional).
Interview: mention the lookup table and that digit count is only O(log n).
❓ FAQ
🔄 Input / output examples
n | Strong? | Reason |
|---|---|---|
145 | Yes | 1! + 4! + 5! = 145 |
2 | Yes | 2! = 2 |
10 | No | 1! + 0! = 2 |
99 | No | 9! + 9! = 725760 |
Edge cases and pitfalls
Most issues come from handling 0 and from recomputing factorials too often.
What should happen?
If you include 0, its digit list is “0”, so the sum is 0! = 1. That means 0 is not strong under the usual “sum equals number” rule. Many beginner problems start at 1 anyway.
Avoid recursion per digit
A recursive factorial function works but repeats the same work for each digit. The array fact[10] is simpler and faster.
Factorials fit comfortably
The largest digit factorial is 9! = 362880, so summing a handful of digits fits in int for normal input sizes. For extreme digit counts, use a wider type.
⏱️ Time and space complexity
| Task | Time | Extra space |
|---|---|---|
Check one n | O(d) where d is number of digits | O(1) |
Scan 1..U | O(U log U) (digit work per number) | O(1) |
Summary
- Definition: strong means the sum of
digit!equals the number. - Code: use a
fact[10]lookup table and extract digits with% 10. - Range 1..200:
1 2 145.
Strong numbers are also called digital factorial numbers. In base 10, the classic examples are 1, 2, 145, and 40585 (because each equals the sum of factorials of its digits).
9 people found this page helpful
