A hyper-cube is a symmetrical object in dimensions that has sides: two opposing sides on each of the axes. Suppose you want to create all possible permutations in which you can paint a hyper-cube using a pallet of colors. All permutations must be different, even under rotation in the dimensional space.
For example, in 2 dimensions, the hyper-cubes are squares and the sides are the 4 (one-dimensional) sides of the square. In 3 dimensions the hyper-cubes are just cubes, with 6 sides, etc. If you paint a hyper-cube of dimensions out of a pallet of 1 color, then you can only make 1 permutation: paint the whole hyper-cube in that one color. If you paint a 2 dimensional hyper-cube using a pallet of 2 colors (lets say red and green) then one square will be all green, one square will be all red, one square will have three green sides and one red side, one square will have three red sides and one green side, and two squares will have two green sides and two red sides, namely one where the two sides of the same color touch and one where they are opposite of each other. So there are in total 6 permutations.
Write a program that calculates the number of permutations when given the number of dimensions ( ) and the number of available colors ( ).
Explicit examples.
Let f(n, k) be the number of permutation in n dimensions and with k available colors. f(2,2) = 6; f(2, 3) = 24; f(3, 2) = 10; f(3, 5) = 800; f(5, 14) = 163332729. Note how the latter ends on 2729.
What is the (minimum) number of dimensions (n) that you need to get a number of permutations that ends on 2018 if you are not allowed to use more than 200 colors?
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
The hardest part of this problem is to realize that in order to make the permutations unique you need to think in terms of n pairs of colors as opposed to 2 n sides . The reason for this is that under rotation all you really do is swap axes. Apart from breaking symmetry (making a mirror image) you can reach every permutation of the axes and their pairs of opposing, coloured sides.
Therefore, the correct approach is as follows:
1) Find the number of symmetrical pairs; that is - pairs whose mirror image is the same - aka, have the same color on each side.
This number (sp) is equal to k : one pair for each color.
2) Find the number of a-symmetrical pairs; that is - pairs whose mirror image is not the same - aka, have different colors on each side.
This number (ap) is equal to k ( k − 1 ) / 2 . Note that we divide by two because the pair red-green is the same as green-red: the sides of a pair are not ordered.
3) Find the number of distinct multisets of n pairs (one for each axis!). This is equivalent to number of combinations with repetition . It is with repetition because multiple axes can have the same pair of colors of course; the pairs need not be distinct. The formula for this "number of combinations of x choose y with repetition" is ( y x + y − 1 ) .
This number (np) is therefore equal to ( n s p + a p + n − 1 ) . Namely, we have in total s p + a p distinct pairs and need to choose n , one for each axis, with repetition ("putting back").
4) Find the number of distinct sets of n pairs that are a-symmetric; whose mirror image is different. That means that each pair must be a-symmetric, but also that each element in the set of pairs must be distinct. So now we need to choose n from the a-symmetrical pairs without repetition. The formula for this "x choose y" or number of y-combinations is ( y x ) .
This number (nap) is therefore ( n a p ) .
5) Find the number of distinct sets of n pairs that are symmetric. This is of course the total number of pairs minus the a-symmetric ones:
This number (nsp) is n p − n a p .
6) Finally we can calculate the number of permutations of the hyper-cube itself.
f ( n , k ) = n s p + 2 × n a p
The reason that the a-symmetrical sets of pairs are counted twice is because their mirror image is different, so they will cause two different hyper-cubes that can not be made equal under rotation.
Note that ( y x ) is short for x ! / y ! ( x − y ) ! .
So, this requires to calculate the factorial of numbers as large as ap. For n=67 that is 2211, which doesn't fit in a normal builtin integer. You can use a big int library, or you can make use of the fact that you only need to know the answer modulo 10000. In that case you can keep all numbers small, also during the calculations. For that to work however you need a more optimized algorithm to calculate the binomial ( y x ) modulo 10000.
Without calculating modulo 10000 you can verify that the number of permutations for n=67 and k=23 is 1614652659246460685133480072022754982197377870904804029782415092786252018 using a bigint library. But that doesn't prove that there isn't some value of k for a smaller value of n. However, since it was given that k < 200 it is doable to calculate all permutations exactly this way for an increasing n for every k up till 200.
The program that I wrote, in C++, to check everything needs to do the calculations modulo 10000. I couldn't figure out how to calculate the binomial without using a bigint library however.
Here is my program:
Which prints