-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Add Kaprekar number checker to special_numbers #12723
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Add Kaprekar number checker to special_numbers #12723
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't 10 be a Kaprekar number, since 10^2 = 100, and "100" = "10" + "0" gives 10?
10 is Disallowed, see above. |
Thanks for the link. Then, the definition should be clarified in the code function description to be: "Kaprekar numbers: positive numbers n such that n = q+r and n^2 = q*10^m+r, for some m >= 1, q >= 0 and 0 <= r < 10^m, with n != 10^a, a >= 1." (https://oeis.org/A006886)? |
for more information, see https://pre-commit.ci
Updated the function to align with the strict definition of Kaprekar numbers per OEIS A006886 and Iannucci (1997). Powers of 10 are now explicitly excluded. Let me know if there's anything else to improve! |
square = str(n**2) | ||
for i in range(1, len(square)): | ||
left, right = square[:i], square[i:] | ||
if int(right) == 0: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems this check is not needed anymore.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just added a comment in code, as the check for int(right) == 0 seems not to be necessary any more after adding explicit check for powers of 10.
Thanks! I removed the int(right) == 0 check as suggested. The new logic excludes powers of 10 directly. Let me know if anything else needs a tweak. |
There was a problem hiding this 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.
import math | ||
|
||
|
||
def is_kaprekar_number(n: int) -> bool: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please provide descriptive name for the parameter: n
All checks are now passing, including tests, Ruff style checks, and namespace packaging. The implementation follows the strict Kaprekar definition and excludes powers of 10 as required. Everything should be ready for merge. Thanks again for the helpful feedback! |
This PR adds a new function
is_kaprekar_number(n)
undermaths/special_numbers/
.The function determines whether a number is a Kaprekar number based on digit-splitting logic, where the square of a number is divided into two parts that sum to the original number. It includes inline doctests to demonstrate usage and ensure correctness.
✔️ This contribution follows all repository contribution guidelines:
n: int -> bool
)doctest.testmod()
🎯 Educational Value:
This function enhances the repository's coverage of special number classifications and provides a clean, beginner-friendly example of digit-based number theory in Python.
Tested and ready for review. Thank you for maintaining this valuable resource!