Files
Abhinav Pandey 152e29034d Improved code readability and code quality (#4663)
* Fixed Small typos :-)

* Update BufferedReader.java

* Made the following changes :

* Improved readability of files and removed gramatical errors.

* Implemented data assigning instead of manually calling arr.ylength in several instances like FindMax, FindMaxRecursion etc.

* Removed unwanted params from several files

* Implemented Math methods in files math/FindMinRecursion.java and FindMaxRecursion.java

* Update src/main/java/com/thealgorithms/maths/FindMinRecursion.java

---------

Co-authored-by: Debasish Biswas <debasishbsws.dev@gmail.com>
2023-10-11 17:29:55 +05:30

58 lines
1.4 KiB
Java

package com.thealgorithms.maths;
/**
* This is Euclid's algorithm, used to find the greatest common
* denominator Override function name gcd
*
* @author Oskar Enmalm 3/10/17
*/
public class GCD {
/**
* get the greatest common divisor
*
* @param num1 the first number
* @param num2 the second number
* @return gcd
*/
public static int gcd(int num1, int num2) {
if (num1 < 0 || num2 < 0) {
throw new ArithmeticException();
}
if (num1 == 0 || num2 == 0) {
return Math.abs(num1 - num2);
}
while (num1 % num2 != 0) {
int remainder = num1 % num2;
num1 = num2;
num2 = remainder;
}
return num2;
}
/**
* @brief computes gcd of an array of numbers
*
* @param numbers the input array
* @return gcd of all of the numbers in the input array
*/
public static int gcd(int[] numbers) {
int result = 0;
for (final var number : numbers) {
result = gcd(result, number);
}
return result;
}
public static void main(String[] args) {
int[] myIntArray = {4, 16, 32};
// call gcd function (input array)
System.out.println(gcd(myIntArray)); // => 4
System.out.printf("gcd(40,24)=%d gcd(24,40)=%d%n", gcd(40, 24), gcd(24, 40)); // => 8
}
}