-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
feat: add count distinct primes from binary string algorithm #2970
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
rudrakshtank
wants to merge
9
commits into
TheAlgorithms:master
Choose a base branch
from
rudrakshtank:add-prime-binary-string
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.
+107
−0
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
55d5f0d
Add files via upload
rudrakshtank 628174d
Delete bit_manipulation/count_distinct_primes_from_binary_string.cpp
rudrakshtank 05f1a9c
Add files via upload
rudrakshtank 789a92c
Merge branch 'master' into add-prime-binary-string
realstealthninja 25a5d73
Changes done in count_distinct_primes_from_binary_string.cpp
rudrakshtank 1213f92
changes done to count_distinct_primes_from_binary_string.cpp
rudrakshtank 6247d96
Merge branch 'master' into add-prime-binary-string
realstealthninja 2c97141
changes done in count_distinct_primes_from_binary_string.cpp
rudrakshtank 3e3e75c
doc: ensure brief for bit_manip name space is the same across files
realstealthninja 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
107 changes: 107 additions & 0 deletions
107
bit_manipulation/count_distinct_primes_from_binary_string.cpp
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 @@ | ||
/** | ||
* @file count_distinct_primes_from_binary_string.cpp | ||
* @brief Count distinct primes formed from binary strings using allowed operations. | ||
* | ||
* @author Rudraksh Tank | ||
* @date July 2025 | ||
* | ||
* @details | ||
* Given a binary string, the task is to count how many distinct prime decimal numbers | ||
* can be formed by: | ||
* - Swapping any two characters (makes position irrelevant) | ||
* - Changing any '1' to '0' (not the reverse) | ||
* | ||
* Efficient solution using bit manipulation and Sieve of Eratosthenes. | ||
* | ||
* Tags: Bit Manipulation, Prime Numbers, Combinatorics, Greedy, Bitmask | ||
*/ | ||
|
||
#include <cstdint> ///< For uint32_t | ||
#include <iostream> ///< For input/output stream handling (std::cin, std::cout) | ||
#include <vector> ///< For dynamic array storage (std::vector) | ||
#include <unordered_set>///< For storing distinct primes without duplicates (std::unordered_set) | ||
#include <algorithm> ///< For algorithms like std::count | ||
#include <cassert> ///< For assert-based testing | ||
|
||
const uint32_t MAX = 1e6; ///< Upper bound for prime sieve | ||
static std::vector<bool> is_prime; | ||
|
||
/** | ||
* @namespace bit_manipulation | ||
* @brief Bit manipulation algorithms | ||
*/ | ||
namespace bit_manipulation { | ||
|
||
/** | ||
* @brief Precomputes all prime numbers up to MAX using Sieve of Eratosthenes. | ||
*/ | ||
void precomputePrimes() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again would recommend using constexpr to do the computation at compile time |
||
is_prime.assign(MAX + 1, true); | ||
is_prime[0] = is_prime[1] = false; | ||
for (uint32_t i = 2; i * i <= MAX; i++) { | ||
if (is_prime[i]) { | ||
for (uint32_t j = i * i; j <= MAX; j += i) { | ||
is_prime[j] = false; | ||
} | ||
} | ||
} | ||
} | ||
|
||
int popcount(uint32_t x) { | ||
int count = 0; | ||
while (x) { | ||
count += x & 1; | ||
x >>= 1; | ||
} | ||
return count; | ||
} | ||
/** | ||
* @brief Counts distinct prime numbers that can be formed from the given binary string. | ||
* @param s Binary string input | ||
* @return Number of distinct primes possible after allowed transformations | ||
*/ | ||
int countPrimeBinaryStrings(const std::string &s) { | ||
int n = s.length(); | ||
int k = std::count(s.begin(), s.end(), '1'); | ||
int cnt = 0; | ||
int limit = 1 << n; | ||
|
||
std::unordered_set<int> seen; | ||
|
||
for (uint32_t i = 2; i < limit; i++) { | ||
if (popcount(i) <= k && is_prime[i]) { | ||
if (!seen.count(i)) { | ||
cnt++; | ||
seen.insert(i); | ||
} | ||
} | ||
} | ||
|
||
return cnt; | ||
} | ||
|
||
} // namespace bit_manipulation | ||
|
||
/** | ||
* @brief Static test function using assertions instead of I/O. | ||
*/ | ||
static void tests() { | ||
using namespace bit_manipulation; | ||
|
||
precomputePrimes(); | ||
|
||
// Example test cases | ||
assert(countPrimeBinaryStrings("1") == 0); // Only "1" -> not prime | ||
assert(countPrimeBinaryStrings("11") == 2); // Should form primes like 3 | ||
assert(countPrimeBinaryStrings("101") == 3); // Can form primes like 5 | ||
assert(countPrimeBinaryStrings("000") == 0); // No 1s -> no primes possible | ||
} | ||
|
||
/** | ||
realstealthninja marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* @brief Main function to run tests. | ||
realstealthninja marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* @returns 0 on successful exit | ||
*/ | ||
int main() { | ||
tests(); | ||
return 0; | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.