From 32a937f626641ca4ea24620d76b3ba355fa5376d Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Sun, 7 Feb 2021 12:42:27 -0500 Subject: [PATCH] Added functions to determine the gcd of 2 nums --- CSClasses/Algorithms.cs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/CSClasses/Algorithms.cs b/CSClasses/Algorithms.cs index 1747384..e073d85 100644 --- a/CSClasses/Algorithms.cs +++ b/CSClasses/Algorithms.cs @@ -705,6 +705,40 @@ namespace mee{ //Return the proper number. The location counter is 1 off of the subscript return fibNums[(fibLoc - 1) % 3]; } + //This function returns the GCD of the two numbers sent to it + public static int GCD(int num1, int num2){ + while((num1 != 0) && (num2 != 0)){ + if(num1 > num2){ + num1 %= num2; + } + else{ + num2 %= num1; + } + } + return num1 | num2; + } + public static long GCD(long num1, long num2){ + while((num1 != 0) && (num2 != 0)){ + if(num1 > num2){ + num1 %= num2; + } + else{ + num2 %= num1; + } + } + return num1 | num2; + } + public static BigInteger GCD(BigInteger num1, BigInteger num2){ + while((num1 != 0) && (num2 != 0)){ + if(num1 > num2){ + num1 %= num2; + } + else{ + num2 %= num1; + } + } + return num1 | num2; + } //This function return sht enumber of times the character occurs in the string public static long FindNumOccurrence(string str, char c){ return str.Count(ch => ch == c);