-
-
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
Open
Sean-Randall
wants to merge
14
commits into
TheAlgorithms:master
Choose a base branch
from
Sean-Randall:add-kaprekar-check
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 11 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
cd7e724
Add Kaprekar number checker to special_numbers
Sean-Randall 7692d03
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 9ed325d
Fix: Clean whitespace and formatting
Sean-Randall 50193cd
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] e523f99
Clarified Kaprekar number definition and excluded powers of 10 (e.g.,…
Sean-Randall 322ed12
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] c33f100
Remove redundant check for int(right) == 0 after excluding powers of 10
Sean-Randall bca915d
Merge branch 'add-kaprekar-check' of https://github.com/Sean-Randall/…
Sean-Randall 15c2bac
Fix Kaprekar logic to exclude powers of 10; add passing tests
Sean-Randall 80b7535
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] d159245
Trigger CI rerun
Sean-Randall 1dd98c2
Apply Ruff style fixes after renaming parameter
Sean-Randall c59e0c8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 597e0f0
Add __init__.py to fix Ruff INP001 implicit namespace error
Sean-Randall File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import math | ||
|
||
|
||
def is_kaprekar_number(n: int) -> bool: | ||
""" | ||
Determine whether a number is a Kaprekar number (excluding powers of 10). | ||
|
||
A Kaprekar number is a positive number n such that: | ||
n^2 = q * 10^m + r, for some m >= 1, q >= 0, 0 <= r < 10^m, | ||
and n = q + r, with the restriction that n is not a power of 10. | ||
|
||
Args: | ||
n (int): The number to check. | ||
|
||
Returns: | ||
bool: True if it's a Kaprekar number, else False. | ||
|
||
Examples: | ||
>>> is_kaprekar_number(45) | ||
True | ||
>>> is_kaprekar_number(9) | ||
True | ||
>>> is_kaprekar_number(10) | ||
False | ||
>>> is_kaprekar_number(1) | ||
True | ||
""" | ||
if n == 1: | ||
return True | ||
if n <= 0 or math.log10(n).is_integer(): | ||
return False # Disallow powers of 10 (e.g., 10, 100, 1000) | ||
|
||
square = str(n**2) | ||
for i in range(1, len(square)): | ||
left, right = square[:i], square[i:] | ||
if n == int(left or "0") + int(right): | ||
return True | ||
return False |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from maths.special_numbers.kaprekar_number import is_kaprekar_number | ||
Check failure on line 1 in tests/special_numbers/test_kaprekar_number.py
|
||
|
||
|
||
def test_kaprekar_numbers(): | ||
assert is_kaprekar_number(1) is True | ||
assert is_kaprekar_number(9) is True | ||
assert is_kaprekar_number(45) is True | ||
assert is_kaprekar_number(55) is True | ||
assert is_kaprekar_number(99) is True | ||
assert is_kaprekar_number(297) is True | ||
|
||
|
||
def test_non_kaprekar_numbers(): | ||
assert is_kaprekar_number(10) is False # Power of 10 | ||
assert is_kaprekar_number(100) is False # Power of 10 | ||
assert is_kaprekar_number(3) is False | ||
assert is_kaprekar_number(50) is False | ||
assert is_kaprekar_number(0) is False | ||
assert is_kaprekar_number(-45) is False |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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