Computing a permuation from its number

  • Thread starter W3bbo
  • Start date
  • Tags
    Computing
In summary, the author is writing a program to generate every possible valid Mastermind code, and is having difficulty generating codes for cases where there cannot be duplicate colors. They are considering a simpler solution of storing the permutations in an array and then using the index of the array as a method of getting the Nth permutation. However, the problem is more interesting mathematically and the author spends time discussing it.
  • #1
W3bbo
31
0
I'm writing a program that generates every possible valid Mastermind code.

That itself is easy. There are 6 colors in 4 possible positions. Cycling through them all is done like so:

Code:
for(int i = 0 to 1295 ) { // 1295 == 6^4 - 1, there are 1296 possible permutations of colors
    
    color1 = ( i / 6 ^ 0 ) % 6;
    color2 = ( i / 6 ^ 1 ) % 6;
    color3 = ( i / 6 ^ 2 ) % 6;
    color4 = ( i / 6 ^ 3 ) % 6;
    
}

where each color is represented by a number ( 0 = red, 1 = blue, etc )

which gives combinations like so:

(begin)
0000
0001
0002
...
3005
3010
...
5554
5555
(end)

So that's all perfectly fine.

Now, how do I do the same but for cases where there cannot be duplicate colors?

I know the size of the set is 6*5*4*3 = 360 elements

So I want my loop to be something like this:

Code:
for(int i=1;i<=360;i++) {
    
    color1 = // what, exactly?
    
}

So how can I convert a number between 1 and 360 into a permutation of colors?

Thanks!
 
Physics news on Phys.org
  • #2
W3bbo said:
I'm writing a program that generates every possible valid Mastermind code.

That itself is easy. There are 6 colors in 4 possible positions. Cycling through them all is done like so:

Code:
for(int i = 0 to 1295 ) { // 1295 == 6^4 - 1, there are 1296 possible permutations of colors
    
    color1 = ( i / 6 ^ 0 ) % 6;
    color2 = ( i / 6 ^ 1 ) % 6;
    color3 = ( i / 6 ^ 2 ) % 6;
    color4 = ( i / 6 ^ 3 ) % 6;
    
}

where each color is represented by a number ( 0 = red, 1 = blue, etc )
It would be even easier to avoid doing anything fancy:
Code:
for(color1 = 0 to 5)
for(color2 = 0 to 5)
for(color3 = 0 to 5)
for(color4 = 0 to 5) {
// do something
}
 
  • #3
Hurkyl said:
It would be even easier to avoid doing anything fancy:
Code:
for(color1 = 0 to 5)
for(color2 = 0 to 5)
for(color3 = 0 to 5)
for(color4 = 0 to 5) {
// do something
}

Easier, yeah; but due to requirements elsewhere in the program I need to be able to derive a permutation from a number.
 
  • #4
From the perspective of computer programming, your question is repulsive since it seems simpler to store the permutations in an array and then use the index of the array as a method of getting the Nth permutation.

However, as a purely mathematical question it does have some interest. To discuss the matter, consider the simpler case of the permuations of {1,2,3}. We can order these by using the convention that the smaller of two permutations is the one that has the smaller first entry and if the first entries are equal, the smaller is the one that has the smaller second entry, etc.

This puts the list in the order of
N
1. (1,2,3)
2. (1,3,2)
3. (2,1,3)
4. (2,3,1)
5. (3,1,2)
6. (3,2,1)

It's clear that we can the first entry in the list by dividing N+1 by 2 using integer division. But how do we get the second entry? Anyone see a simple formula for the progression 2,3,1,3,1,2 ?

An interesting number theory question is the following: Given a finite string of digits base M, do there exist constants A and B such that (A + B*N) mod M is equal to the Nth digit in the string?

Another thought is to look for a formula that uses the valule of icolor1 to aid in finding icolor2.
 
  • #5
The direct path to answering the question originally asked, I think, is to apply exactly same trick that leads to the first for-loop in the opening post, with index from 0 to 1295.

Of course, one is unlikely to work through the details if they're unwilling to take the first step and write down the obvious solution to the problem.


Honestly, I think this is an issue of premature optimization -- the opening poster is spending far more effort trying to come up with an elegant solution, clever, or (alledgedly) efficient solution, rather than just implement a dumb solution that is more than adequate for the task.


I take pride in the fact I've written more bubble sorts than any other kind. :smile: I can write them quicker than other sorts, and it let's me focus my time on the parts of the program that really matter, rather than shaving a few microseconds off of my sorting by using a different method.

(I would, of course, spend more time on the sort if I was actually sorting a huge list, or doing the sort a huge number of times. But even then I might wait until I've confirmed it's taking a sizable proportion of my program's time)
 
Last edited:

1. What is a permutation?

A permutation is an arrangement of a set of objects in a specific order. It is a way of rearranging objects or elements in a group without repetition.

2. How is a permutation represented?

A permutation can be represented in several ways, such as using numbers, symbols, or letters. For example, the permutation of {1, 2, 3} can be represented as 123, 132, 213, 231, 312, or 321.

3. What is the process of computing a permutation from its number?

The process of computing a permutation from its number involves finding the total number of permutations and then using a formula to determine the specific permutation. The formula is n! / (n-r)! where n is the total number of objects and r is the number of objects in each permutation.

4. Can a permutation be calculated without repetition?

Yes, a permutation can be calculated without repetition. This is known as a combination, where the order of the elements does not matter. The formula for computing a combination is n! / (r! * (n-r)!).

5. What is the significance of computing a permutation from its number?

Computing a permutation from its number is important in various fields such as mathematics, statistics, and computer science. It allows for the efficient organization and manipulation of data, and is used in various algorithms and equations to solve complex problems.

Similar threads

  • Set Theory, Logic, Probability, Statistics
Replies
14
Views
938
  • Set Theory, Logic, Probability, Statistics
Replies
6
Views
1K
  • Set Theory, Logic, Probability, Statistics
Replies
10
Views
2K
  • Precalculus Mathematics Homework Help
Replies
21
Views
638
  • Precalculus Mathematics Homework Help
Replies
8
Views
428
  • Set Theory, Logic, Probability, Statistics
Replies
12
Views
1K
  • Precalculus Mathematics Homework Help
Replies
7
Views
1K
  • General Math
Replies
2
Views
886
  • Precalculus Mathematics Homework Help
Replies
2
Views
859
  • Precalculus Mathematics Homework Help
Replies
2
Views
1K
Back
Top