-
-
Notifications
You must be signed in to change notification settings - Fork 49k
added DSA problem of rare_number #13679
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
bibhukesh22
wants to merge
3
commits into
TheAlgorithms:master
Choose a base branch
from
bibhukesh22:master
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 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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,107 @@ | ||
| """ | ||
| Rare numbers are special non-palindromic numbers N such that both N + rev(N) | ||
| and N - rev(N) are perfect squares, where rev(N) is the reverse of the digits | ||
| of N. | ||
|
|
||
| This module provides functions to check and generate rare numbers in a given | ||
| range. Rare numbers are useful in number theory and pattern-based algorithm | ||
| analysis. | ||
|
|
||
| For more information about rare numbers, refer to: | ||
| [https://www.geeksforgeeks.org/dsa/rare-numbers/](https://www.geeksforgeeks.org/dsa/rare-numbers/) | ||
| """ | ||
|
|
||
| import math | ||
|
|
||
|
|
||
| def rare_numbers(start: int, end: int) -> list[int]: | ||
| """ | ||
| Find all rare numbers between the given start and end range (inclusive). | ||
|
|
||
| A rare number N satisfies both: | ||
| 1. N + rev(N) is a perfect square. | ||
| 2. N - rev(N) is a perfect square. | ||
| where rev(N) is the reverse of the digits of N, | ||
| and N must not be a palindrome. | ||
|
|
||
| Args: | ||
| start (int): The lower bound of the range (inclusive). | ||
| end (int): The upper bound of the range (inclusive). | ||
|
|
||
| Returns: | ||
| list[int]: A list of rare numbers within the specified range. | ||
|
|
||
| Raises: | ||
| ValueError: If start or end is negative, or start > end. | ||
|
|
||
| Examples: | ||
| >>> rare_numbers(-1, 100) | ||
| Traceback (most recent call last): | ||
| ... | ||
| ValueError: Range limits must be non-negative and start <= end | ||
| >>> rare_numbers(1, 100) | ||
| [] | ||
| >>> rare_numbers(1, 1000) | ||
| [65] | ||
| """ | ||
| if start < 0 or end < 0 or start > end: | ||
| raise ValueError("Range limits must be non-negative and start <= end") | ||
|
|
||
| rares = [] | ||
| for n in range(start, end + 1): | ||
| rev_n = _reverse_number(n) | ||
| if n == rev_n: | ||
| continue # skip palindromes | ||
| if _is_perfect_square(n + rev_n) and _is_perfect_square(abs(n - rev_n)): | ||
| rares.append(n) | ||
| return rares | ||
|
|
||
|
|
||
| def _reverse_number(num: int) -> int: | ||
| """ | ||
| Reverse the digits of a given integer. | ||
|
|
||
| Args: | ||
| num (int): The integer to reverse. | ||
|
|
||
| Returns: | ||
| int: The reversed integer. | ||
|
|
||
| Examples: | ||
| >>> _reverse_number(123) | ||
| 321 | ||
| >>> _reverse_number(400) | ||
| 4 | ||
| """ | ||
| rev = 0 | ||
| while num > 0: | ||
| rev = rev * 10 + num % 10 | ||
| num //= 10 | ||
| return rev | ||
|
|
||
|
|
||
| def _is_perfect_square(n: int) -> bool: | ||
| """ | ||
| Check if a number is a perfect square. | ||
|
|
||
| Args: | ||
| n (int): The number to check. | ||
|
|
||
| Returns: | ||
| bool: True if n is a perfect square, False otherwise. | ||
|
|
||
| Examples: | ||
| >>> _is_perfect_square(16) | ||
| True | ||
| >>> _is_perfect_square(20) | ||
| False | ||
| """ | ||
| if n < 0: | ||
| return False | ||
| root = int(math.isqrt(n)) | ||
|
Check failure on line 101 in maths/rare_number.py
|
||
| return root * root == n | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| import doctest | ||
| doctest.testmod() | ||
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