From 1ebff0b1e41a2d782f344e2c0f542b86e7dbfa24 Mon Sep 17 00:00:00 2001 From: errorgorn Date: Fri, 25 Oct 2019 16:41:49 +0800 Subject: [PATCH 1/2] Added deterministic version to miller rabin algorithm --- .../miller_rabin_deterministic.cpp | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 Math/miller_rabin_primality_test/miller_rabin_deterministic.cpp diff --git a/Math/miller_rabin_primality_test/miller_rabin_deterministic.cpp b/Math/miller_rabin_primality_test/miller_rabin_deterministic.cpp new file mode 100644 index 00000000..f43674d6 --- /dev/null +++ b/Math/miller_rabin_primality_test/miller_rabin_deterministic.cpp @@ -0,0 +1,81 @@ +#include +#include +#include +using namespace std; +const int witness[]={2,3,5,7}; +const int witness_size=4; ///remember to edit this value too +/* Note that different witness arrays will cause the function to have different accuracies. +But according to wikipedia: https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test + + if n < 2,047, it is enough to test a = 2; + if n < 1,373,653, it is enough to test a = 2 and 3; + if n < 9,080,191, it is enough to test a = 31 and 73; + if n < 25,326,001, it is enough to test a = 2, 3, and 5; + if n < 3,215,031,751, it is enough to test a = 2, 3, 5, and 7; (I am using this) + if n < 4,759,123,141, it is enough to test a = 2, 7, and 61; + if n < 1,122,004,669,633, it is enough to test a = 2, 13, 23, and 1662803; + if n < 2,152,302,898,747, it is enough to test a = 2, 3, 5, 7, and 11; + if n < 3,474,749,660,383, it is enough to test a = 2, 3, 5, 7, 11, and 13; + if n < 341,550,071,728,321, it is enough to test a = 2, 3, 5, 7, 11, 13, and 17. + + Note that for the correctness of the algorithm, the witness array should only be filled with prime numbers. +*/ + +bool isPrime(int i){ + ///check for the simple cases + if (i==1) return false; + if (i==2) return true; + if (!(i&1)) return false; + for (int x=0;x pow; ///this holds the bits of i with all the trailing zeros striped off + int s=0; ///number of trailing zeros + + int _i=i-1; + while (!(_i&1)){ + s++; + _i>>=1; + } + + while (_i){ + pow.push_back(_i&1); + _i>>=1; + } + + long long curr; + for (int x=0;x::reverse_iterator it=pow.rbegin();it!=pow.rend();it++){ + if (*it){ + curr=(curr*curr)%i; + curr=(curr*witness[x])%i; + } + else{ + curr=(curr*curr)%i; + } + } + + if (curr==1 || curr==i-1) goto _end; + + for (int x=1;x Date: Fri, 25 Oct 2019 16:55:55 +0800 Subject: [PATCH 2/2] replaced functions to count number of bits to __builtin_popcount refer to https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html for more details --- ..._of_bits_to_be_flipped_to_convert_a_to_b.cpp | 15 ++------------- BitManipulation/count_ones/count_ones.cpp | 17 ++++++----------- 2 files changed, 8 insertions(+), 24 deletions(-) diff --git a/BitManipulation/count_number_of_bits_to_be_flipped_to_convert_a_to_b/count_number_of_bits_to_be_flipped_to_convert_a_to_b.cpp b/BitManipulation/count_number_of_bits_to_be_flipped_to_convert_a_to_b/count_number_of_bits_to_be_flipped_to_convert_a_to_b.cpp index be64de7f..acf95ec8 100644 --- a/BitManipulation/count_number_of_bits_to_be_flipped_to_convert_a_to_b/count_number_of_bits_to_be_flipped_to_convert_a_to_b.cpp +++ b/BitManipulation/count_number_of_bits_to_be_flipped_to_convert_a_to_b/count_number_of_bits_to_be_flipped_to_convert_a_to_b.cpp @@ -1,26 +1,15 @@ #include using namespace std; -int countSetBits(int n) { - int setBits = 0; - while(n > 0){ - if(n&1){ - setBits++; - } - n>>=1; - } - return setBits; -} - int main(){ int a,b; cin>>a>>b; // take the XOR of both the numbers and then count the set bits in that XOR int _xor = a^b; - int answer = countSetBits(_xor); + int answer = __builtin_popcount(_xor); cout<<"number of bits to be flipped "< -using namespace std; +using namespace std; short count_ones(unsigned int x) { - short num_bits = 0; - while (x) { - num_bits += x & 1; // mask one - x >>= 1; - } - return num_bits; + return __builtin_popcount(x); } -int main() { +int main() { - int number = 10; // use any number - short result = count_ones(number); - cout << "result: " << result << endl; + int number = 10; // use any number + short result = count_ones(number); + cout << "result: " << result << endl; return 0; }