-
-
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
base: master
Are you sure you want to change the base?
Changes from 4 commits
55d5f0d
628174d
05f1a9c
789a92c
25a5d73
1213f92
6247d96
2c97141
3e3e75c
9be8bb3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,77 @@ | ||||||||
/** | ||||||||
* @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 <iostream> | ||||||||
#include <vector> | ||||||||
#include <unordered_set> | ||||||||
#include <algorithm> | ||||||||
|
||||||||
const int MAX = 1e6; | ||||||||
|
const int MAX = 1e6; | |
const uint32_t MAX = 1e6; | |
Outdated
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.
std::vector<bool> is_prime; | |
static std::vector<bool> is_prime; | |
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.
Since you want to pre compute primes why not make this a constexpr? and assign in it directly? instead of a call from main. Also afaik vectors cant be used as constexpr in c++17 hence it might be preferred if you use std::array instead.
if any of this sounds confusing let me know I can explain or direct you to resources to learn about this feature of c++
rudrakshtank marked this conversation as resolved.
Show resolved
Hide resolved
Outdated
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.
__builtin_popcount is not part of the STL library it only exists in GCC.
thus this should be replaced with std::popcount()
if (__builtin_popcount(i) <= k && is_prime[i]) { | |
if (std::popcount(i) <= k && is_prime[i]) { | |
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.
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 add documentation for all the above headers as shown in the example