From 525cafea949d33f841fa4daab8a28fe116cb5c2c Mon Sep 17 00:00:00 2001 From: Shrikar17 <54152124+Shrikar17@users.noreply.github.com> Date: Wed, 4 Dec 2019 13:54:48 +0530 Subject: [PATCH] Added factorisation technique (#604) * Added factorisation technique * Update and rename Math/hcf.txt to math/greatest_common_divisor.cpp * Update greatest_common_divisor.cpp --- math/greatest_common_divisor.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 math/greatest_common_divisor.cpp diff --git a/math/greatest_common_divisor.cpp b/math/greatest_common_divisor.cpp new file mode 100644 index 000000000..5601c4be9 --- /dev/null +++ b/math/greatest_common_divisor.cpp @@ -0,0 +1,27 @@ +// C++ program to find GCD of two numbers +#include + +// Recursive function to return gcd of a and b +int gcd(int a, int b) { + // Everything divides 0 + if (a == 0) + return b; + if (b == 0) + return a; + + // base case + if (a == b) + return a; + + // a is greater + if (a > b) + return gcd(a-b, b); + return gcd(a, b-a); +} + +// Driver program to test above function +int main() { + int a = 98, b = 56; + std::cout << "GCD of " << a << " and " << b << " is " << gcd(a, b); + return 0; +}