@Test public void testDivisors() { for (int i=0; i<10000; i++) { final int[] divisors = divisors(i); assertTrue(XArray.isStrictlySorted(divisors)); for (int j=0; j<divisors.length; j++) { assertEquals(0, i % divisors[j]); } if (i == 0){ assertEquals(0, divisors.length); } else { assertEquals(1, divisors[0]); assertEquals(i, divisors[divisors.length - 1]); } } assertTrue(Arrays.equals(divisors(2000), new int[] { 1, 2, 4, 5, 8, 10, 16, 20, 25, 40, 50, 80, 100, 125, 200, 250, 400, 500, 1000, 2000 })); assertTrue(Arrays.equals(divisors(4331), new int[] { 1, 61, 71, 4331 })); assertTrue(Arrays.equals(divisors(7800), new int[] { 1, 2, 3, 4, 5, 6, 8, 10, 12, 13, 15, 20, 24, 25, 26, 30, 39, 40, 50, 52, 60, 65, 75, 78, 100, 104, 120, 130, 150, 156, 195, 200, 260, 300, 312, 325, 390, 520, 600, 650, 780, 975, 1300, 1560, 1950, 2600, 3900, 7800 })); }
public static int[] divisors(int number) { if (number == 0) { return new int[0]; } number = Math.abs(number); int[] divisors = new int[16]; divisors[0] = 1; int count = 1; final int sqrt = (int) (Math.sqrt(number) + 1E-6); for (int p,i=0; (p=primeNumber(i)) <= sqrt; i++) { if (number % p == 0) { if (count == divisors.length) { divisors = XArray.resize(divisors, count*2); } divisors[count++] = p; } } int source = count; if (count*2 > divisors.length) { divisors = XArray.resize(divisors, count*2); } int d1 = divisors[--source]; int d2 = number / d1; if (d1 != d2) { divisors[count++] = d2; } while (--source >= 0) { divisors[count++] = number / divisors[source]; } for (int i=1; i<count; i++) { d1 = divisors[i]; for (int j=i; j<count; j++) { d2 = d1 * divisors[j]; if (number % d2 == 0) { int p = org.geotools.resources.Java6.binarySearch(divisors, j, count, d2); if (p < 0) { p = ~p; if (count == divisors.length) { divisors = XArray.resize(divisors, count*2); } System.arraycopy(divisors, p, divisors, p+1, count-p); divisors[p] = d2; count++; } } } } divisors = XArray.resize(divisors, count); assert XArray.isStrictlySorted(divisors); return divisors; }
XMath { public static int[] divisors(int number) { if (number == 0) { return new int[0]; } number = Math.abs(number); int[] divisors = new int[16]; divisors[0] = 1; int count = 1; final int sqrt = (int) (Math.sqrt(number) + 1E-6); for (int p,i=0; (p=primeNumber(i)) <= sqrt; i++) { if (number % p == 0) { if (count == divisors.length) { divisors = XArray.resize(divisors, count*2); } divisors[count++] = p; } } int source = count; if (count*2 > divisors.length) { divisors = XArray.resize(divisors, count*2); } int d1 = divisors[--source]; int d2 = number / d1; if (d1 != d2) { divisors[count++] = d2; } while (--source >= 0) { divisors[count++] = number / divisors[source]; } for (int i=1; i<count; i++) { d1 = divisors[i]; for (int j=i; j<count; j++) { d2 = d1 * divisors[j]; if (number % d2 == 0) { int p = org.geotools.resources.Java6.binarySearch(divisors, j, count, d2); if (p < 0) { p = ~p; if (count == divisors.length) { divisors = XArray.resize(divisors, count*2); } System.arraycopy(divisors, p, divisors, p+1, count-p); divisors[p] = d2; count++; } } } } divisors = XArray.resize(divisors, count); assert XArray.isStrictlySorted(divisors); return divisors; } }
XMath { public static int[] divisors(int number) { if (number == 0) { return new int[0]; } number = Math.abs(number); int[] divisors = new int[16]; divisors[0] = 1; int count = 1; final int sqrt = (int) (Math.sqrt(number) + 1E-6); for (int p,i=0; (p=primeNumber(i)) <= sqrt; i++) { if (number % p == 0) { if (count == divisors.length) { divisors = XArray.resize(divisors, count*2); } divisors[count++] = p; } } int source = count; if (count*2 > divisors.length) { divisors = XArray.resize(divisors, count*2); } int d1 = divisors[--source]; int d2 = number / d1; if (d1 != d2) { divisors[count++] = d2; } while (--source >= 0) { divisors[count++] = number / divisors[source]; } for (int i=1; i<count; i++) { d1 = divisors[i]; for (int j=i; j<count; j++) { d2 = d1 * divisors[j]; if (number % d2 == 0) { int p = org.geotools.resources.Java6.binarySearch(divisors, j, count, d2); if (p < 0) { p = ~p; if (count == divisors.length) { divisors = XArray.resize(divisors, count*2); } System.arraycopy(divisors, p, divisors, p+1, count-p); divisors[p] = d2; count++; } } } } divisors = XArray.resize(divisors, count); assert XArray.isStrictlySorted(divisors); return divisors; } private  XMath(); }
XMath { public static int[] divisors(int number) { if (number == 0) { return new int[0]; } number = Math.abs(number); int[] divisors = new int[16]; divisors[0] = 1; int count = 1; final int sqrt = (int) (Math.sqrt(number) + 1E-6); for (int p,i=0; (p=primeNumber(i)) <= sqrt; i++) { if (number % p == 0) { if (count == divisors.length) { divisors = XArray.resize(divisors, count*2); } divisors[count++] = p; } } int source = count; if (count*2 > divisors.length) { divisors = XArray.resize(divisors, count*2); } int d1 = divisors[--source]; int d2 = number / d1; if (d1 != d2) { divisors[count++] = d2; } while (--source >= 0) { divisors[count++] = number / divisors[source]; } for (int i=1; i<count; i++) { d1 = divisors[i]; for (int j=i; j<count; j++) { d2 = d1 * divisors[j]; if (number % d2 == 0) { int p = org.geotools.resources.Java6.binarySearch(divisors, j, count, d2); if (p < 0) { p = ~p; if (count == divisors.length) { divisors = XArray.resize(divisors, count*2); } System.arraycopy(divisors, p, divisors, p+1, count-p); divisors[p] = d2; count++; } } } } divisors = XArray.resize(divisors, count); assert XArray.isStrictlySorted(divisors); return divisors; } private  XMath(); static double pow10(final double x); static strictfp double pow10(final int x); static int sgn(final double x); static int sgn(final float x); static int sgn(long x); static int sgn(int x); static short sgn(short x); static byte sgn(byte x); static double roundIfAlmostInteger(final double value, int maxULP); static double trimDecimalFractionDigits(final double value, final int maxULP, int n); static int countDecimalFractionDigits(final double value); static float toNaN(int index); static synchronized int primeNumber(final int index); static int[] divisors(int number); }
XMath { public static int[] divisors(int number) { if (number == 0) { return new int[0]; } number = Math.abs(number); int[] divisors = new int[16]; divisors[0] = 1; int count = 1; final int sqrt = (int) (Math.sqrt(number) + 1E-6); for (int p,i=0; (p=primeNumber(i)) <= sqrt; i++) { if (number % p == 0) { if (count == divisors.length) { divisors = XArray.resize(divisors, count*2); } divisors[count++] = p; } } int source = count; if (count*2 > divisors.length) { divisors = XArray.resize(divisors, count*2); } int d1 = divisors[--source]; int d2 = number / d1; if (d1 != d2) { divisors[count++] = d2; } while (--source >= 0) { divisors[count++] = number / divisors[source]; } for (int i=1; i<count; i++) { d1 = divisors[i]; for (int j=i; j<count; j++) { d2 = d1 * divisors[j]; if (number % d2 == 0) { int p = org.geotools.resources.Java6.binarySearch(divisors, j, count, d2); if (p < 0) { p = ~p; if (count == divisors.length) { divisors = XArray.resize(divisors, count*2); } System.arraycopy(divisors, p, divisors, p+1, count-p); divisors[p] = d2; count++; } } } } divisors = XArray.resize(divisors, count); assert XArray.isStrictlySorted(divisors); return divisors; } private  XMath(); static double pow10(final double x); static strictfp double pow10(final int x); static int sgn(final double x); static int sgn(final float x); static int sgn(long x); static int sgn(int x); static short sgn(short x); static byte sgn(byte x); static double roundIfAlmostInteger(final double value, int maxULP); static double trimDecimalFractionDigits(final double value, final int maxULP, int n); static int countDecimalFractionDigits(final double value); static float toNaN(int index); static synchronized int primeNumber(final int index); static int[] divisors(int number);  }