Skip to content
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

GCD implementation using Euclid's algorithm #14

Closed
wants to merge 1 commit into from
Closed
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
8 changes: 8 additions & 0 deletions include/BigInt.hpp
Original file line number Diff line number Diff line change
@@ -93,6 +93,14 @@ class BigInt {
// I/O stream operators:
friend std::istream& operator>>(std::istream&, BigInt&);
friend std::ostream& operator<<(std::ostream&, const BigInt&);


// gcd:
friend BigInt gcd(const BigInt& num1, const BigInt& num2);
friend BigInt gcd(const BigInt& num1, const long long& num2);
friend BigInt gcd(const BigInt& num1, const std::string& num2);
Copy link
Owner

Choose a reason for hiding this comment

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

The GCD functions don't need to be friends of BigInt as they can be implemented without accessing the private members.

friend BigInt gcd(const long long& num1, const BigInt& num2);
friend BigInt gcd(const std::string& num1, const BigInt& num2);

// Conversion functions:
std::string to_string() const;
70 changes: 70 additions & 0 deletions include/operators/binary_arithmetic.hpp
Original file line number Diff line number Diff line change
@@ -530,4 +530,74 @@ BigInt operator%(const std::string& lhs, const BigInt& rhs) {
return BigInt(lhs) % rhs;
}

/*
Logic for handling negative case:

gcd{a,b}=gcd{|a|,b}=gcd{a,|b|}=gcd{|a|,|b|}

Alternatively, this can be put:

gcd{a,b}=gcd{−a,b}=gcd{a,−b}=gcd{−a,−b}, which follows directly from the above. https://proofwiki.org/wiki/GCD_for_Negative_Integers
*/


/*
Gcd (BigInt, BigInt) :
--------------------
*/

BigInt gcd(const BigInt &num1, const BigInt &num2){
BigInt abs_num1 = abs(num1);
BigInt abs_num2 = abs(num2);

// Trivial Case handling
if(abs_num1.value == "0") return abs_num2;
else if (abs_num2.value == "0") return abs_num1;
Copy link
Owner

Choose a reason for hiding this comment

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

You can do:

if (abs_num1 == 0)
    return abs_num2;
if (abs_num2 == 0)
    return abs_num1;


BigInt temp = 1;

while(abs_num2 != 0){
temp = abs_num1 % abs_num2;
abs_num1 = abs_num2;
abs_num2 = temp;
}

return abs_num1;
}


/*
Gcd (BigInt, long long) :
--------------------
*/
BigInt gcd(const BigInt& num1, const long long& num2){
return gcd(num1, BigInt(num2));
}

/*
Gcd (BigInt, string) :
--------------------
*/
BigInt gcd(const BigInt& num1, const std::string& num2){
return gcd(num1, BigInt(num2));
}

/*
Gcd (long long, BigInt) :
--------------------
*/
BigInt gcd(const long long& num1, const BigInt& num2){
return gcd(num2, BigInt(num1));
}


/*
Gcd (string, BigInt) :
--------------------
*/
BigInt gcd(const std::string& num1, const BigInt& num2){
return gcd(num2, BigInt(num1));
}


#endif // BIG_INT_BINARY_ARITHMETIC_OPERATORS_HPP