From e5a1e3d55f43da568fa83494403a62f2de927b7b Mon Sep 17 00:00:00 2001 From: Mayur Kulkarni Date: Mon, 29 May 2017 12:48:06 +0530 Subject: [PATCH] Added AKS primality test --- Utils/NumberUtils.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Utils/NumberUtils.java b/Utils/NumberUtils.java index f6bc894..20b9725 100644 --- a/Utils/NumberUtils.java +++ b/Utils/NumberUtils.java @@ -247,6 +247,25 @@ public static BigDecimal bigSquareRoot(BigInteger val) { return bigSquareRoot(new BigDecimal(val)); } + /** + * Checks if number is prime using AKS Primality test + * + * @param n the number + * @return true if its prime, false otherwise + */ + public static boolean isPrimeAKS(long n) { + if (n <= 1) return false; + if (n <= 3) return true; + + if (n % 2 == 0 || n % 3 == 0) return false; + + for (int i = 5; i * i <= n; i = i + 6) + if (n % i == 0 || n % (i + 2) == 0) + return false; + + return true; + } + public double euclideanDist(int x1, int x2, int y1, int y2) { return Math.sqrt(Math.abs(x1 - x2) * Math.abs(x1 - x2) + Math.abs(y1 - y2) * Math.abs(y1 - y2));