-
Notifications
You must be signed in to change notification settings - Fork 27
Added Number_Theory_Algorithms under include directory #44
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mention details about the algorithms
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| 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 | ||
|
|
||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
|
||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ? |
||
|
|
||
| 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 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,9 +26,13 @@ namespace dsa | |
| count = size; | ||
| capacity=size+spare; | ||
| objs = new type[capacity]; | ||
| if(!objs) | ||
| throw "Memory allocation failed"; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
@@ -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++) | ||
|
|
@@ -125,6 +134,8 @@ namespace dsa | |
| count = 0; | ||
| capacity = spare; | ||
| objs = new type[capacity]; | ||
| if(!objs) | ||
| throw "Memory allocation failed"; | ||
| } | ||
| }; | ||
| } | ||
|
|
||
There was a problem hiding this comment.
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