Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 10 commits into TheAlgorithms:master
base: master
Choose a base branch
Loading
from rudrakshtank:add-prime-binary-string

Conversation

Copy link

@rudrakshtank rudrakshtank commented Jul 19, 2025
edited
Loading

@realstealthninja Could you please review this PR?

Description of Change

Added count_distinct_primes_from_binary_string.cpp to the bit_manipulation/ directory.

This program counts the number of distinct prime numbers that can be formed from a binary string using:

  • Any permutation of characters (i.e., swapping)
  • Changing '1's to '0's (but not the reverse)

It uses an efficient implementation of the Sieve of Eratosthenes for prime checking and bitmasking to explore all possible combinations.

Includes:

  • Doxygen-style documentation
  • A working main() with an example test case

Contributors guide


Checklist

  • Added description of change
  • File name matches File name guidelines
  • Added test case/example in main()
  • Added Doxygen-style documentation
  • Comments/documentation added as needed
  • PR title follows semantic commit guidelines
  • Confirmed this is not a duplicate contribution
  • I acknowledge that all my contributions will be made under the project's license

Notes:

Efficient bitmask + sieve-based solution to find all valid prime numbers from binary strings using allowed transformations.

#include <unordered_set>
#include <algorithm>

const int MAX = 1e6;
Copy link
Collaborator

@realstealthninja realstealthninja Aug 19, 2025

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

Copy link
Collaborator

@realstealthninja realstealthninja Aug 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you still havent done this

Suggested change
const int MAX = 1e6;
const uint32_t MAX = 1e6;

#include <algorithm>

const int MAX = 1e6;
std::vector<bool> is_prime;
Copy link
Collaborator

@realstealthninja realstealthninja Aug 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
std::vector<bool> is_prime;
staticstd::vector<bool> is_prime;

Copy link
Collaborator

@realstealthninja realstealthninja Aug 19, 2025

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++

std::unordered_set<int> seen;

for (int i = 2; i < limit; i++) {
if (__builtin_popcount(i) <= k && is_prime[i]) {
Copy link
Collaborator

@realstealthninja realstealthninja Aug 19, 2025

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()

Suggested change
if (__builtin_popcount(i) <= k && is_prime[i]) {
if (std::popcount(i) <= k && is_prime[i]) {

Comment on lines 19 to 22
#include <iostream>
#include <vector>
#include <unordered_set>
#include <algorithm>
Copy link
Collaborator

@realstealthninja realstealthninja Aug 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#include <iostream>
#include <vector>
#include <unordered_set>
#include <algorithm>
#include <bit> /// for std::popcount
#include <iostream>
#include <vector>
#include <unordered_set>
#include <algorithm>

Copy link
Collaborator

@realstealthninja realstealthninja Aug 24, 2025

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

@realstealthninja realstealthninja changed the title (削除) Add count distinct primes from binary string algorithm (削除ここまで) (追記) feat: add count distinct primes from binary string algorithm (追記ここまで) Aug 19, 2025
Copy link
Author

Thank you for the review, I'll get back to you with suggested changes

realstealthninja reacted with thumbs up emoji

Copy link
Author

I have done with the suggested changes, please review the changes

Comment on lines 71 to 77
void tests(){
precomputePrimes();
std::string s;
std::cin >> s;
std::cout << countPrimeBinaryStrings(s) << std::endl;
}

Copy link
Collaborator

@realstealthninja realstealthninja Aug 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tests should be outside the namespace and static, A test should be comparing a computed value to a known value using an assert statement

#include <unordered_set>
#include <algorithm>

const int MAX = 1e6;
Copy link
Collaborator

@realstealthninja realstealthninja Aug 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you still havent done this

Suggested change
const int MAX = 1e6;
const uint32_t MAX = 1e6;

Comment on lines 19 to 22
#include <iostream>
#include <vector>
#include <unordered_set>
#include <algorithm>
Copy link
Collaborator

@realstealthninja realstealthninja Aug 24, 2025

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


// Example test cases
assert(countPrimeBinaryStrings("1") == 0); // Only "1" -> not prime
assert(countPrimeBinaryStrings("11") > 0); // Should form primes like 3
Copy link
Collaborator

@realstealthninja realstealthninja Aug 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you need to compare against the actual value that should be forming, comparing this way against 0 is too general. here from my estimation
01 not prime
10 prime
11 prime
so the answer should be three
you should be comparing against three

// Example test cases
assert(countPrimeBinaryStrings("1") == 0); // Only "1" -> not prime
assert(countPrimeBinaryStrings("11") > 0); // Should form primes like 3
assert(countPrimeBinaryStrings("101") > 0); // Can form primes like 5
Copy link
Collaborator

@realstealthninja realstealthninja Aug 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing here, saying any binary string which is non zero and non 1 can form primes above 0 is not a great test.
here i estimate
001 not prime
100 not prime
101 prime

so the answer should be one, please compare against the actual number

/**
* @brief Precomputes all prime numbers up to MAX using Sieve of Eratosthenes.
*/
void precomputePrimes() {
Copy link
Collaborator

@realstealthninja realstealthninja Sep 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again would recommend using constexpr to do the computation at compile time

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Reviewers

@Panquesito7 Panquesito7 Awaiting requested review from Panquesito7 Panquesito7 is a code owner

@realstealthninja realstealthninja Awaiting requested review from realstealthninja realstealthninja is a code owner

1 more reviewer

@anandfresh anandfresh anandfresh approved these changes

Reviewers whose approvals may not affect merge requirements

Requested changes must be addressed to merge this pull request.

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /