Recursively constructs a bit string with a pre-specified number of 1 bits based on a unique and consistent ordering of the space of such possible strings. - Java java.lang

Java examples for java.lang:Math Number

Description

Recursively constructs a bit string with a pre-specified number of 1 bits based on a unique and consistent ordering of the space of such possible strings.

Demo Code


//package com.java2s;
import java.math.BigInteger;

public class Main {
    public static final BigInteger ZERO = new BigInteger("0", 10);
    public static final BigInteger ONE = new BigInteger("1", 10);

    /**/*from w  w w  . j  a va  2  s  .co m*/
     * Recursively constructs a bit string with a pre-specified number of 1 bits
     * based on a unique and consistent ordering of the space of such possible strings.
     * @param ba storage space for the bit string
     * @param i length of the bit string
     * @param j number of 1 bits in the string
     * @param n a number in the range of [1, C(i, j)] specifying which of the C(i,j) bit strings to return 
     * and where C(i, j) is the binomial coefficient, i choose j
     * @return
     */
    private static byte[] getNthBitstring(byte[] ba, int i, int j,
            BigInteger n) {
        if (i > Integer.MAX_VALUE || j > Integer.MAX_VALUE)
            throw new IllegalArgumentException();
        BigInteger l = binomialCoefficientAsBigInteger(i, j);
        if (n.compareTo(l) == 1)
            throw new ArrayIndexOutOfBoundsException();
        if (j == 0)
            return ba;
        else if (i == j) {
            for (int z = 0; z < i; z++)
                ba[z] = 1;
            return ba;
        } else if (i > j) {
            BigInteger k = binomialCoefficientAsBigInteger(i - 1, j - 1);
            if (n.compareTo(k) != 1) {
                ba[(int) i - 1] = 1;
                return getNthBitstring(ba, i - 1, j - 1, n);
            } else {
                ba[(int) i - 1] = 0;
                return getNthBitstring(ba, i - 1, j, n.subtract(k));
            }
        } else
            throw new ArrayIndexOutOfBoundsException();
    }

    /**
     * Computes the binomial coefficient (number of unique ways to choose k elements from a set of n elements) by converting arguments from type long to type BigInteger. 
     * @param n number of elements in the set
     * @param k number of elements to choose from the set
     * @return number of ways to choose k elements from n elements in the set
     */
    public static BigInteger binomialCoefficientAsBigInteger(long n, long k) {
        return binomialCoefficient(new BigInteger(Long.toString(n), 10),
                new BigInteger(Long.toString(k), 10));
    }

    /**
     * Computes the binomial coefficient (number of unique ways to choose k elements from a set of n elements) over BigIntegers.
     * @param n number of elements in the set
     * @param k number of elements to choose from the set
     * @return number of ways to choose k elements from n elements in the set
     */
    public static BigInteger binomialCoefficient(BigInteger n, BigInteger k) {
        BigInteger result = ONE;
        if (k.compareTo(n) > 0)
            return ZERO;
        for (BigInteger i = ONE; i.compareTo(k) < 1; i = i.add(ONE)) {
            result = result.multiply(n); //result *= n--;
            n = n.subtract(ONE);
            result = result.divide(i); //result /= i;
        }
        return result;
    }

    /**
     * Computes the binomial coefficient (number of unique ways to choose k elements from a set of n elements).
     * @param n number of elements in the set
     * @param k number of elements to choose from the set
     * @return number of ways to choose k elements from n elements in the set
     */
    public static long binomialCoefficient(long n, long k) {
        long result = 1;
        if (k > n)
            return 0;
        for (long i = 1; i <= k; i++) {
            result *= n--;
            result /= i;
        }
        return result;
    }

    /**
     * Represents an array of an array of ints (an int matrix) as a matrix style formatted string
     * @param a array of an array of ints to be formatted
     * @return a String representing the input array as a matrix
     */
    public static String toString(int[][] a) {
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a[i].length; j++) {
                buf.append(a[i][j]);
                buf.append(" ");
            }
            buf.replace(buf.length() - 1, buf.length(), "\n");
        }
        return buf.substring(0, buf.length() - 1);
    }
}

Related Tutorials