how to generate Narcissistic numbers faster?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
A narcissistic number (Armstrong number) is a number equal to the sum of its digits each raised to the number of digits. A naive generator tests every number and recomputes digit powers repeatedly, which becomes slow as range grows. Faster generation comes from precomputing powers, pruning impossible candidates, and reducing repeated digit extraction work.
For practical ranges, these optimizations provide significant speedups while keeping implementation simple.
Core Sections
1. Baseline definition and naive check
Works for correctness but does repeated exponentiation and string conversion for every candidate.
2. Precompute digit powers
This removes repeated exponentiation in hot loops.
3. Prune search space by digit length bounds
For digit length k, max possible sum is k * 9^k. If this is smaller than smallest k-digit number, no candidates exist for larger k beyond a point. Use this to cap search length.
4. Use digit-combination generation
Instead of iterating all integers, generate digit multisets and compute power sums directly, then validate digit composition. This is more complex but much faster for large ranges.
5. Parallelize safely for wide ranges
Ranges can be partitioned across workers because checks are independent. Combine results at end in sorted order.
Common Pitfalls
- Recomputing
d ** kinside inner loops for every candidate. - Using slow string operations where numeric digit extraction suffices.
- Ignoring digit-length pruning and scanning impossible ranges.
- Forgetting to verify candidate digit composition in multiset-based approaches.
- Parallelizing without deterministic merge and duplicate handling.
Summary
Faster narcissistic number generation comes from reducing repeated work: precompute digit powers, prune impossible digit lengths, and avoid unnecessary conversions. For larger intervals, digit-combination methods and parallel execution provide additional gains. Start with table-based optimization, then move to advanced pruning only if performance demands it.
A practical way to keep this guidance useful in real projects is to convert it into an executable runbook rather than leaving it as one-time reading. A strong runbook lists exact prerequisites, expected versions, environment assumptions, and a short sequence of checks that confirm healthy behavior. It also records the first one or two failure signatures engineers are most likely to see and maps each signature to the next diagnostic step. This structure reduces ambiguity when incidents happen under time pressure and helps new contributors act with the same consistency as experienced maintainers.
It also helps to keep one minimal reproducible fixture in version control for this exact scenario. The fixture can be a tiny script, API call, YAML manifest, query, or test harness that demonstrates both expected success and a known failure mode. When dependencies, frameworks, or infrastructure versions change, that fixture becomes an early warning system for regressions. Instead of discovering breakage deep in production workflows, teams can run a focused check in minutes and isolate whether the problem is environmental drift, configuration mismatch, or logic change.
For long-term reliability, add one lightweight automated guardrail to CI that targets the most fragile point in the workflow. Good candidates include schema validation, deterministic unit tests, protocol compatibility checks, API contract tests, and startup smoke tests. Keep the guardrail narrow and fast so it runs on every change and produces actionable output when it fails. If the same issue class appears repeatedly, promote the manual troubleshooting step into automation. Over time, this shifts effort from reactive debugging to preventive quality control, and ensures the article stays aligned with how teams actually build, test, and operate software.
Related reading
- How to generate random graphs?
- How to generate random numbers biased towards one value in a range?
- How to generate Sudoku boards with unique solutions
- How to generate the power-set of a given List?
- How to get a normal distribution within a range in numpy?
- How to get all algebraic associative operations on a finite set by efficient algorithm?
- How to get a specific sequence like this?
- How to get access of individual trees of a xgboost model in python /R

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.