-
-
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?
feat: add count distinct primes from binary string algorithm #2970
Conversation
Add count_distinct_primes_from_binary_string.cpp
#include <unordered_set> | ||
#include <algorithm> | ||
|
||
const int MAX = 1e6; |
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.
max cannot be a negative number, hence replace int with uint32_t
#include <algorithm> | ||
|
||
const int MAX = 1e6; | ||
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.
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++
|
||
return cnt; | ||
} | ||
|
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.
} // bit_manipulation |
std::unordered_set<int> seen; | ||
|
||
for (int i = 2; i < limit; i++) { | ||
if (__builtin_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.
__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]) { | |
#include <iostream> | ||
#include <vector> | ||
#include <unordered_set> | ||
#include <algorithm> |
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.
#include <iostream> | |
#include <vector> | |
#include <unordered_set> | |
#include <algorithm> | |
#include <bit> /// for std::popcount | |
#include <iostream> | |
#include <vector> | |
#include <unordered_set> | |
#include <algorithm> |
return cnt; | ||
} | ||
|
||
/** |
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 tests here
Thank you for the review, I'll get back to you with suggested changes |
I have done with the suggested changes, please review the changes |
@realstealthninja Could you please review this PR?
Description of Change
Added
count_distinct_primes_from_binary_string.cpp
to thebit_manipulation/
directory.This program counts the number of distinct prime numbers that can be formed from a binary string using:
It uses an efficient implementation of the Sieve of Eratosthenes for prime checking and bitmasking to explore all possible combinations.
Includes:
main()
with an example test caseContributors guide
Checklist
main()
Notes:
Efficient bitmask + sieve-based solution to find all valid prime numbers from binary strings using allowed transformations.