Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions dsalglib/dsalglib.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@
#include "include/heap.h"
#include "include/graph.h"

#include "include/Number_Theory_Algorithms/fundamental_functions.h"
Copy link
Owner

Choose a reason for hiding this comment

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

please change name of directory to be small case

#include "include/Number_Theory_Algorithms/prime_numbers.h"

#endif //DSALGLIB_DSALGLIB_H
63 changes: 63 additions & 0 deletions dsalglib/include/Number_Theory_Algorithms/fundamental_functions.h
Original file line number Diff line number Diff line change
@@ -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) {
Copy link
Owner

Choose a reason for hiding this comment

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

Consider effects of bits when dealing with small number but not similar data type

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
Copy link
Owner

Choose a reason for hiding this comment

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

Mention details about the algorithms

Copy link
Owner

Choose a reason for hiding this comment

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

Maybe the reference used to implement this particular algo.

*/
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
53 changes: 53 additions & 0 deletions dsalglib/include/Number_Theory_Algorithms/prime_numbers.h
Original file line number Diff line number Diff line change
@@ -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

Copy link
Owner

Choose a reason for hiding this comment

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

remove blank line

/*
* 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

Copy link
Owner

Choose a reason for hiding this comment

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

ditto

/*
* 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<bool> sieve(long long int number) {
Copy link
Owner

Choose a reason for hiding this comment

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

can this use some sort of optimisation ? making is_prime static or similar other optimisations ?
maybe a object which can be reset with different limit later that again internally just increases length of array


array<bool> 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
15 changes: 13 additions & 2 deletions dsalglib/include/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ namespace dsa
count = size;
capacity=size+spare;
objs = new type[capacity];
if(!objs)
throw "Memory allocation failed";
Copy link
Owner

Choose a reason for hiding this comment

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

need to do this across lib. a wrapper around new should be implemented in this, could you please file a ticket for that and we can take it up.

else {
long long int i;
for(i=0;i<count;i++)
objs[i]=param;
for(i=0;i<count;i++)
objs[i]=param;
}
}


Expand All @@ -38,6 +42,11 @@ namespace dsa

type *old = objs;
objs = new type[newsize];

if(!objs) {
throw "Memory allocation failed";
return;
}

long long int temp;
for(temp=0;temp<count;temp++)
Expand Down Expand Up @@ -125,6 +134,8 @@ namespace dsa
count = 0;
capacity = spare;
objs = new type[capacity];
if(!objs)
throw "Memory allocation failed";
}
};
}
Expand Down
25 changes: 25 additions & 0 deletions dsalglib/tests.h
Original file line number Diff line number Diff line change
Expand Up @@ -249,4 +249,29 @@ void trie_test(){
cout << t.search("third") << "\n";
}

void num_theory_functions_test() {
cout << binaryExponentiation(4, 3) << "\n";
cout << binaryExponentiation(0, 4) << "\n";
cout << binaryExponentiation(5, 0) << "\n";

const long long mod = 1e9 + 7;
long long a = 2, b = 1e9;
cout << modularBinaryExponentiation(a, b, mod) << "\n";
}

void num_theory_primes_test() {
cout << isNumPrime(7) << "\n";
cout << isNumPrime(1) << "\n";
cout << isNumPrime(1299653) << "\n";

try {
array<bool> 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