Skip to content

Commit 1213f92

Browse files
authored
changes done to count_distinct_primes_from_binary_string.cpp
1 parent 25a5d73 commit 1213f92

File tree

1 file changed

+32
-19
lines changed

1 file changed

+32
-19
lines changed

bit_manipulation/count_distinct_primes_from_binary_string.cpp

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,31 @@
1616
* Tags: Bit Manipulation, Prime Numbers, Combinatorics, Greedy, Bitmask
1717
*/
1818

19-
#include <bit>
20-
#include <iostream>
21-
#include <vector>
22-
#include <unordered_set>
23-
#include <algorithm>
19+
#include <bit> ///< For std::popcount (bit manipulation utilities)
20+
#include <iostream> ///< For input/output stream handling (std::cin, std::cout)
21+
#include <vector> ///< For dynamic array storage (std::vector)
22+
#include <unordered_set>///< For storing distinct primes without duplicates (std::unordered_set)
23+
#include <algorithm> ///< For algorithms like std::count
24+
#include <cassert> ///< For assert-based testing
2425

25-
const int MAX = 1e6;
26+
const uint32_t MAX = 1e6; ///< Upper bound for prime sieve
2627
static std::vector<bool> is_prime;
2728

2829
/**
2930
* @namespace bit_manipulation
30-
* @brief Bit manipulation algorithms
31+
* @brief Bit manipulation algorithms and prime preprocessing
3132
*/
3233
namespace bit_manipulation {
3334

35+
/**
36+
* @brief Precomputes all prime numbers up to MAX using Sieve of Eratosthenes.
37+
*/
3438
void precomputePrimes() {
3539
is_prime.assign(MAX + 1, true);
3640
is_prime[0] = is_prime[1] = false;
37-
for (int i = 2; i * i <= MAX; i++) {
41+
for (uint32_t i = 2; i * i <= MAX; i++) {
3842
if (is_prime[i]) {
39-
for (int j = i * i; j <= MAX; j += i) {
43+
for (uint32_t j = i * i; j <= MAX; j += i) {
4044
is_prime[j] = false;
4145
}
4246
}
@@ -67,17 +71,26 @@ namespace bit_manipulation {
6771

6872
return cnt;
6973
}
70-
71-
void tests(){
72-
precomputePrimes();
73-
std::string s;
74-
std::cin >> s;
75-
std::cout << countPrimeBinaryStrings(s) << std::endl;
76-
}
77-
78-
} //bit manipulation
74+
75+
} // namespace bit_manipulation
76+
77+
/**
78+
* @brief Static test function using assertions instead of I/O.
79+
*/
80+
static void tests() {
81+
using namespace bit_manipulation;
82+
83+
precomputePrimes();
84+
85+
// Example test cases
86+
assert(countPrimeBinaryStrings("1") == 0); // Only "1" -> not prime
87+
assert(countPrimeBinaryStrings("11") > 0); // Should form primes like 3
88+
assert(countPrimeBinaryStrings("101") > 0); // Can form primes like 5
89+
assert(countPrimeBinaryStrings("000") == 0); // No 1s -> no primes possible
90+
}
91+
7992
/**
80-
* @brief Main function to test the algorithm.
93+
* @brief Main function to run tests.
8194
*/
8295
int main() {
8396
tests();

0 commit comments

Comments
 (0)