Skip to content

Conversation

@AliAlimohammadi
Copy link

Description

Adds a dynamic programming implementation to find all narcissistic numbers (also known as Armstrong numbers) up to a specified limit.

A narcissistic number is a number that equals the sum of its own digits each raised to the power of the number of digits. For example: 153 = 1³ + 5³ + 3³.

Implementation Details

  • Uses memoization to cache digit power calculations (digit^power)
  • Avoids redundant computations when checking multiple numbers
  • Implements true dynamic programming with overlapping subproblems

Dynamic Programming Approach

Overlapping Subproblems: Many numbers share the same digits, so the same digit powers are computed repeatedly (e.g., 153, 351, 135 all need 1³, 5³, 3³)

Memoization: Cache stores (power, digit) -> result mappings to avoid recomputation

Optimal Substructure: The narcissistic property is built from individual digit power results

Examples

>>> find_narcissistic_numbers(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> find_narcissistic_numbers(1000)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407]

>>> find_narcissistic_numbers(10000)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474]

Type of Change

  • Add an algorithm

Checklist

  • I have read CONTRIBUTING.md
  • This pull request is all my own work -- I have not plagiarized
  • I know that pull requests will not be merged if they fail the automated tests
  • This PR only changes one algorithm file
  • All new Python files are placed inside an existing directory (dynamic_programming/)
  • Filename is in all lowercase characters with no spaces or dashes (narcissistic_number.py)
  • All functions and variable names follow Python naming conventions (snake_case)
  • All function parameters and return values are annotated with Python type hints
  • All functions have doctests that pass the automated testing (7 tests)
  • Algorithm includes a URL pointing to Wikipedia or similar explanation
  • Code uses Python 3.13+ features where appropriate
  • Code passes ruff check linting
  • Code is formatted with black (or follows PEP 8)

References

@AliAlimohammadi
Copy link
Author

@poyea

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds a new algorithm to find narcissistic numbers (Armstrong numbers) using a memoization-based approach. A narcissistic number equals the sum of its own digits each raised to the power of the number of digits (e.g., 153 = 1³ + 5³ + 3³). The implementation uses a cache to store digit power calculations to avoid redundant computations.

Key Changes

  • Implements find_narcissistic_numbers() function that finds all narcissistic numbers below a given limit
  • Uses memoization to cache digit^power calculations across different numbers
  • Includes comprehensive doctests covering edge cases and various limit values

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@algorithms-keeper algorithms-keeper bot added the require tests Tests [doctest/unittest/pytest] are required label Dec 15, 2025
Copy link

@algorithms-keeper algorithms-keeper bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

@algorithms-keeper algorithms-keeper bot added the awaiting reviews This PR is ready to be reviewed label Dec 15, 2025
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copy link

@algorithms-keeper algorithms-keeper bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copy link

@algorithms-keeper algorithms-keeper bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

@AliAlimohammadi
Copy link
Author

@poyea, revisions applied.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed require tests Tests [doctest/unittest/pytest] are required

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants