Skip to content

Instantly share code, notes, and snippets.

@elvismetaphor
Created December 6, 2016 10:33
Show Gist options
  • Save elvismetaphor/8d076b1570fac299a3fc2c785b0e46c9 to your computer and use it in GitHub Desktop.
Save elvismetaphor/8d076b1570fac299a3fc2c785b0e46c9 to your computer and use it in GitHub Desktop.
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class CombinationCounter {
private static List<BigInteger> generateCombinationLimitedBy(int index) {
List<BigInteger> sequence = new ArrayList<>();
sequence.add(BigInteger.ONE);
final BigInteger two = new BigInteger("2");
final BigInteger three = new BigInteger("3");
for (int i = 1; i < index; i++) {
for (int j = 0; j <= i; j++) {
sequence.add(two.pow(j).multiply(three.pow(i - j)));
}
}
Collections.sort(sequence, new Comparator<BigInteger>() {
@Override
public int compare(BigInteger big1, BigInteger big2) {
return big1.compareTo(big2);
}
});
return sequence;
}
public static void main(String[] args) {
int index = Integer.parseInt(args[0]);
List<BigInteger> sequence = generateCombinationLimitedBy(index);
System.out.println("The " + index + "th number : " + sequence.get(index));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment