patternjavaMinor
Simple GCD utility in Java
Viewed 0 times
gcdsimpleutilityjava
Problem
I previously discussed the performance concerns about different GCD algorithms. I wrote a simple Java class that implements the Binary GCD algorithm. It is a really tiny class that has only two easy-to-use methods. See for yourself:
GCD.java
*
* @author Subhomoy Haldar
* @version 1.0
*/
public class GCD {
/**
* Private constructor to prevent instantiation.
*/
private GCD() {
throw new Error("Instantiation not allowed.");
}
/**
* This method computes the GCD of two positive integers using the Binary
* GCD algorithm.
*
* This method does not complain if the integers provided are negative.
* In such a case, it simply the absolute value and performs the
* computation. If one of the arguments is zero, the other argument is
* returned. If both are zero, then zero is returned.
*
* @param a The first positive integer.
* @param b The second positive integer.
* @return The GCD of two positive integers.
*/
public static long of(long a, long b) {
// corner cases
if (a == 0) return b;
if (b == 0) return a;
if (a >>= a0;
b >>>= b0;
while (a != b) {
if (a > b) {
a -= b;
a >>>= Long.numberOfTrailingZeros(a);
} else {
b -= a;
b >>>= Long.numberOfTrailingZeros(b);
}
}
ret
GCD.java
package math;
/**
* This is a utility class that provides the functionality of calculating the
* GCD or Greatest Common Divisor (also known as HCF or Highest Common
* Factor) of two or more integers.
*
* It has only two methods:
*
* {@link #of(long, long)}
* {@link #of(long, long, long...)}
*
* Usage is very simple:
*
* ...
* // assuming a, b, c, d and e are integers (long, int, etc.)
* long gcd1 = GCD.of(a, b);
* long gcd2 = GCD.of(a, b, c);
* long gcd3 = GCD.of(a, b, c, d);
* long gcd4 = GCD.of(a, b, c, e);
* ...
**
* @author Subhomoy Haldar
* @version 1.0
*/
public class GCD {
/**
* Private constructor to prevent instantiation.
*/
private GCD() {
throw new Error("Instantiation not allowed.");
}
/**
* This method computes the GCD of two positive integers using the Binary
* GCD algorithm.
*
* This method does not complain if the integers provided are negative.
* In such a case, it simply the absolute value and performs the
* computation. If one of the arguments is zero, the other argument is
* returned. If both are zero, then zero is returned.
*
* @param a The first positive integer.
* @param b The second positive integer.
* @return The GCD of two positive integers.
*/
public static long of(long a, long b) {
// corner cases
if (a == 0) return b;
if (b == 0) return a;
if (a >>= a0;
b >>>= b0;
while (a != b) {
if (a > b) {
a -= b;
a >>>= Long.numberOfTrailingZeros(a);
} else {
b -= a;
b >>>= Long.numberOfTrailingZeros(b);
}
}
ret
Solution
Instead of throwing an
An
An
(The only way this error will be triggered is if you try to create an instance within this class.)
As this class has only private constructors,
it cannot be extended:
derived classes would need to call a constructor in the super-class,
but none are available.
To make the intention perfectly clear that this class is not designed to be extended, it's good to make it
Error, an AssertionError would be better.An
Error is too broad.An
AssertionError conveys better that instantiating is an error by the programmer, violating a precondition.(The only way this error will be triggered is if you try to create an instance within this class.)
private GCD() {
throw new AssertionError("Instantiation not allowed.");
}As this class has only private constructors,
it cannot be extended:
derived classes would need to call a constructor in the super-class,
but none are available.
To make the intention perfectly clear that this class is not designed to be extended, it's good to make it
final.Code Snippets
private GCD() {
throw new AssertionError("Instantiation not allowed.");
}Context
StackExchange Code Review Q#107809, answer score: 6
Revisions (0)
No revisions yet.