From 516f83e067e6bf636eb507058a10c33552f62400 Mon Sep 17 00:00:00 2001 From: gmungoc Date: Thu, 5 Oct 2017 15:17:19 +0100 Subject: [PATCH] JAL-2738 helper method for convolving mappings --- src/jalview/util/MathUtils.java | 22 ++++++++++++++++++++++ test/jalview/util/MathUtilsTest.java | 26 ++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 src/jalview/util/MathUtils.java create mode 100644 test/jalview/util/MathUtilsTest.java diff --git a/src/jalview/util/MathUtils.java b/src/jalview/util/MathUtils.java new file mode 100644 index 0000000..72d46a2 --- /dev/null +++ b/src/jalview/util/MathUtils.java @@ -0,0 +1,22 @@ +package jalview.util; + +public class MathUtils +{ + + /** + * Returns the greatest common divisor of two integers + * + * @param a + * @param b + * @return + */ + public static int gcd(int a, int b) + { + if (b == 0) + { + return Math.abs(a); + } + return gcd(b, a % b); + } + +} diff --git a/test/jalview/util/MathUtilsTest.java b/test/jalview/util/MathUtilsTest.java new file mode 100644 index 0000000..fc84741 --- /dev/null +++ b/test/jalview/util/MathUtilsTest.java @@ -0,0 +1,26 @@ +package jalview.util; + +import static org.testng.Assert.assertEquals; + +import org.testng.annotations.Test; + +public class MathUtilsTest +{ + @Test + public void testGcd() + { + assertEquals(MathUtils.gcd(0, 0), 0); + assertEquals(MathUtils.gcd(0, 1), 1); + assertEquals(MathUtils.gcd(1, 0), 1); + assertEquals(MathUtils.gcd(1, 1), 1); + assertEquals(MathUtils.gcd(1, -1), 1); + assertEquals(MathUtils.gcd(-1, 1), 1); + assertEquals(MathUtils.gcd(2, 3), 1); + assertEquals(MathUtils.gcd(4, 2), 2); + assertEquals(MathUtils.gcd(2, 4), 2); + assertEquals(MathUtils.gcd(2, -4), 2); + assertEquals(MathUtils.gcd(-2, 4), 2); + assertEquals(MathUtils.gcd(-2, -4), 2); + assertEquals(MathUtils.gcd(2 * 3 * 5 * 7 * 11, 3 * 7 * 13 * 17), 3 * 7); + } +} -- 1.7.10.2