diff --git a/dsalglib/dsalglib.h b/dsalglib/dsalglib.h index dffb23a..9b6ff5a 100644 --- a/dsalglib/dsalglib.h +++ b/dsalglib/dsalglib.h @@ -17,4 +17,7 @@ #include "include/heap.h" #include "include/graph.h" +#include "include/Number_Theory_Algorithms/fundamental_functions.h" +#include "include/Number_Theory_Algorithms/prime_numbers.h" + #endif //DSALGLIB_DSALGLIB_H diff --git a/dsalglib/include/Number_Theory_Algorithms/fundamental_functions.h b/dsalglib/include/Number_Theory_Algorithms/fundamental_functions.h new file mode 100644 index 0000000..1923a28 --- /dev/null +++ b/dsalglib/include/Number_Theory_Algorithms/fundamental_functions.h @@ -0,0 +1,63 @@ +/* + * Created by yadvendra20 on 3/12/2020 +*/ + +#ifndef DSALGLIB_NUMTHEORY_FF_H +#define DSALGLIB_NUMTHEORY_FF_H + +#include "../alginc.h" + +namespace dsa { + + /* + * The binary exponentiation method computes + * powers directed by the exponent bits, one + * at a time. Bits of value 0 require a squaring; + * bits of value 1 require a squaring and a multiplication. + * + * More about binary exponentiantion: https://cp-algorithms.com/algebra/binary-exp.html + */ + + long long binaryExponentiation(long long base, long long exponent) { + if(base == 0LL) + return 0LL; + long long result = 1LL; + while(exponent > 0) { + if(exponent%2LL == 1LL) + result *= base; + base *= base; + exponent /= 2LL; + } + return result; + } + + /* + * Modular binary exponentiation + */ + long long modularBinaryExponentiation(long long base, long long exponent, long long mod) { + if(base == 0LL) + return 0LL; + long long result = 1LL; + while(exponent > 0) { + if(exponent%2LL == 1LL) + result = (result * base)%mod; + base = (base * base)%mod; + exponent /= 2LL; + } + return result; + } + + /* + * GCD Computation + */ + + long long gcd(long long first_num, long long second_num) { + while(second_num > 0) { + first_num %= second_num; + swapit(first_num, second_num); + } + return first_num; + } +} + +#endif // DSALGLIB_NUMTHEORY_FF_H diff --git a/dsalglib/include/Number_Theory_Algorithms/prime_numbers.h b/dsalglib/include/Number_Theory_Algorithms/prime_numbers.h new file mode 100644 index 0000000..f0807b9 --- /dev/null +++ b/dsalglib/include/Number_Theory_Algorithms/prime_numbers.h @@ -0,0 +1,53 @@ +/* + * Created by yadvendra20 on 3/12/2020 +*/ + +#ifndef DSALGLIB_NUMTHEORY_PRIMES_H +#define DSALGLIB_NUMTHEORY_PRIMES_H + +#include "../array.h" + +namespace dsa { + + // returns true if the number is prime otherwise returns false + + /* + * The complexity of the isNumPrime function is O(square_root(number)) + */ + + bool isNumPrime(int number) { + if(number <= 1) + return false; + for(int i = 2; i * i <= number; i++) { + if(number % i == 0) + return false; + } + return true; + } + + // Sieve of eratosthenes + + /* + * The sieve function returns a boolean array is_prime of 'number' size + * where is_prime[num] = true (if num is a prime number) otherwise false + * + * More about sieve of eratosthenes: https://cp-algorithms.com/algebra/sieve-of-eratosthenes.html + */ + + array sieve(long long int number) { + + array is_prime(number + 1, true); + is_prime[0] = false; + is_prime[1] = false; + + for(int i = 2; i * i <= number; i++) { + if(is_prime[i]) { + for(int j = i * i; j <= number; j += i) + is_prime[j] = false; + } + } + return is_prime; + } +} + +#endif // DSALGLIB_NUMTHEORY_PRIMES_H diff --git a/dsalglib/include/array.h b/dsalglib/include/array.h index c419d8f..b3b4b76 100644 --- a/dsalglib/include/array.h +++ b/dsalglib/include/array.h @@ -26,9 +26,13 @@ namespace dsa count = size; capacity=size+spare; objs = new type[capacity]; + if(!objs) + throw "Memory allocation failed"; + else { long long int i; - for(i=0;i is_prime = sieve(100); + for(int i = 0; i < 100; i++) + if(is_prime[i]) + cout << i << " "; + } catch(const char* exception) { + cout << exception << "\n"; + } +} + #endif //DSALGLIB_TESTS_H