Skip to content

JNumberTools V1.0.0

Deepesh Patel edited this page Jul 13, 2021 · 1 revision

JNumberTools is the open source java-library of combinatorics and number-theory

Currently Available Algorithms

  1. Permutations (7 different types of permutation generation)
  2. Combinations (4 different types of combination generation)
  3. Set/subset generations
  4. Factoradics sequence generation.
  5. Combinadics sequence generation

Permutation Generation Examples:

# Type of Permutation Description API Example Example Output Count
1. Unique Permutations All permutations of size equal to input in lex order of indices of input NumberTools
.permutationsOf("A","B","C")
.unique()
.forEach(System.out::println);
[A, B, C]
[A, C, B]
[B, A, C]
[B, C, A]
[C, A, B]
[C, B, A]
n!
2. Nth Unique Permutation Generate permutations with repeated values starting from 0th permutation(input) and
then generate every nth permutation in lexicographical order of indices of input values.
This is important because say, if we need to generate next 100 trillionth permutation
of 100 items then it will take months to compute if we go sequentially and then skip
the unwanted permutations because the total # of permutations is astronomical
(100!= 9.3326E X 10157)
NumberTools
.permutationsOf("A","B","C")
.uniqueNth(2)
.forEach(System.out::println);
[A, B, C]
[B, A, C]
[C, A, B]
n! / m
3. Unique Permutation of Size k Generates all unique permutations of size k.
In number theory this is also know as k-permutations
NumberTools
.permutationsOf("A","B","C")
.k(2)
.forEach(System.out::println);
[A, B]
[B, A]
[A, C]
[C, A]
[B, C]
[C, B]
nPk
4. Nth Unique Permutation of Size k Generates permutations of unique values of size k,
skipping to every nth permutation in lex order.
NumberTools
.permutationsOf("A","B","C")
.kNth(2,3)
.forEach(System.out::println);
[A, B]
[A, C]
[B, C]
nPk / m
5. Repetitive Permutations of size r This is same as generating base-n numbers of max size r-digits
with given n-symbols, starting from zero in lex order
NumberTools
.permutationsOf("A","B","C")
.repetitive(2)
.forEach(System.out::println);
[A, A]
[A, B]
[A, C]
[B, A]
[B, B]
[B, C]
[C, A]
[C, B]
[C, C]
nr
6. Nth Repetitive Permutation
of size r
This is same as generating AP series of base-n numbers with given n-symbols
and a=0, d=m and with max-digits =r
NumberTools
.permutationsOf("A","B","C")
.repetitiveNth(2,3)//size 2 and m=3
.forEach(System.out::println);
[A, A]
[B, A]
[C, A]
nr/m
7. Repetitive Permutations
with limited supply
Also known as multiset-permutation.
This is special case of repetitive permutation where every item say,
a1,a2,a3 ... has an associated number s1,s2,s3... which denotes how many
times an item can be repeated in a permutation
JJNumberTools.permutationsOf("A", "B")
.repetitiveWithSupply(3,1)//a 3 times
.forEach(System.out::println);
[A, A, A, B]
[A, A, B, A]
[A, B, A, A]
[B, A, A, A]
( ∑ ai . si )! / Π(si!)

Combination Generation Examples:

# Type of Combination Description API Output Count
1. Unique Combination of size r Selection of r distinct items out of n items
In mathematics this is known as n-Choose-r
JNumberTools.combinationsOf("A","B","C")
.unique(2)
.forEach(System.out::println);
[A, B]
[A, C]
[B, C]
nCr
2. Nth Unique Combination of size r Same as n-Choose-r but generating evey nth combination in lex order.
This is important because count of combinations can grow astronomically
large and to generate say, next 1 billionth combination of 34Choose17,
we do not like to wait for few 100 hours to generate billion combinations
sequentially and then selecting billionth combination.
JNumberTools.combinationsOf("A","B","C")
.uniqueNth(2,2)
.forEach(System.out::println);
[A, B]
[B, C]
nCr / m
3. Repetitive Combination of size r. Generates combinations of repeated values od size r. JNumberTools.combinationsOf("A","B")
.repetitive(3)
.forEach(System.out::println);
[A, A, A]
[A, A, B]
[A, B, B]
[B, B, B]
n+r-1Cr
4. Repetitive Combination
with limited supply
Also known as multiset-combination.
This is special case of repetitive combination where every item say,
a1,a2,a3 ... has an associated number s1,s2,s3... which denotes how many
times an item can be repeated in a combination
JNumberTools.combinationsOf("A","B","C")
.repetitiveWithSupply(2,2,1,1) // size 2 and supply 2,1,1
.forEach(System.out::println);
[A, A]
[A, B]
[A, C]
[B, C]
Subset Generation Examples:
Type of set API Output Count
All subsets JNumberTools.subsetsOf("A","B","C")
.all()
.forEach(System.out::println);
[]
[A]
[B]
[C]
[A, B]
[A, C]
[B, C]
[A, B, C]
2n
Subsets in size range from a to b JNumberTools.subsetsOf("A","B","C")
.inRange(2,3)
.forEach(System.out::println);
[A, B]
[A, C]
[B, C]
[A, B, C]
nCi for i= a to b

**Factoradic (Factorial Number System): Following code will generate factoradic numbers starting from 3 to 6.

class Example {
    public static void main(String[] args) {
        Iterator<Factoradic> itr  = NumberTools
                .factoradic()
                .from(3)
                .build().iterator();

        //will print factoradic for 3,4 5 and 6
        for(int i=1; i<=4; i++) {
            System.out.print(itr.next() + " ");
        }    
    }
}

**Combinadic (Combinatorial Number System): Following code will generate combinadics from 10 to 15 of degree 5.

class Example {
    public static void main(String[] args) {
        Iterable<Combinadic> iterable = JNumberTools.combinadic(5, 10, 15);

        //print combinadics of degree 5, from 10 to 15 
        for(Combinadic c: iterable) {
            System.out.println(c);
        }
    }
}

Coming Soon

  1. Nth repetitive permutation with limited supply
  2. Nth repetitive combination
  3. Nth repetitive combination with limited supply
  4. Mixed Radix Number System
  5. Number Partition
  6. Cartesian Product
Clone this wiki locally