# Looking for a simple way to generate some permutation-like numbers!

Asked by BonusQuestion (1482) September 24th, 2010

Given a few set of positive integers, I am trying to generate some numbers using some sort of programming .

Lets say we have three sets of integers of the same size. I would like to pick a permutation f on the first set and form a number by putting the values of f at i next to each other. Then apply the same permutation to the next set and then the last set and finally get an integer by putting these 3 numbers one after another.

For example lets say we have three sets of integers of size three, say {1, 2, 3}, {4,5,6} and {7,8,9} I would like to generate the following numbers:

123456789 (corresponding to the permutation that fixes all numbers)

132465798 (corresponding to the permutation that fixes 1 and switche 2 and 3)

321654987 (corresponding to the permutation that fixes 2 and switches 1 and 3)

213546879 (corresponding to the permutation that fixes 3 and switches 1 and 2)

231564897 (corresponding to the permutation that sends 1 to 2, 2 to 3 and 3 to 1)

312645978 (corresponding to the permutation that sends 1 to 3, 3 to 2 and 2 to 1)

How can I go about generating such numbers either online or with an accessible program?

Observing members: 0 Composing members: 0

If you can take the time to learn it, Pure Data might be just what you are looking for. It is pretty good at managing number sequences and creating output, which appears to be what you are looking for…

Good luck! :)

Arp (3516)

I would check out what Wolfram Alpha can do with combinatorics. Anything beyond what it can do and I would strong recommend giving Python a shot. Here’s something to get you started.

timtrueman (5744)

This claims to work for sets up to size 7: Online Permutations Calculator.

ratboy (15142)

@Arp Thank you. I will look into that.
@timtrueman Thanks but I don’t think Mathematica could help. I am pretty familiar with Wolfram softwares.
@ratboy Thank you but that’s not what I am looking for.

BonusQuestion (1482)

There is an algorithm for generating permutations where each new permutation is generated by exchanging two adjacent elements of the previous one. I would think this would be helpful for you. If you returned the index values (or single value since they are adjacent) you could use this to generate the next permutation for each of your individual cases. You could probably find a description of the algorithm in a book on combinatorial algorithms, which is how I learned about it.

If you are interested, I could send you a description of the algorithm and C# code for it.

@BonusQuestion It seems like the simplest way would be to create a matrix where each row corresponded to an individual set of integers. Then you could permute the columns and read the numbers off. Or is that not comprehensive enough for what you’re trying to do?

finkelitis (1907)

Hurrah for computer science! I just whipped up a PHP class that is capable of generating all the numbers you described. If you’re interested in the source code, check it out here. It doesn’t use matrices @finkelitis although that’s a great idea. It’s basically an adaptation of the Exeter University algorithm I found here. It works by recursively switching items in the array, starting from position 0 and going to n. I set up three arrays, each containing one of the sets of three numbers you mentioned above, permuted each one, then joined them together to produce the final product. I can make this into a hosted XML web service or just a web page if you’d like so you can use it without having to host it somewhere on your own.

camertron (2102)

Okay, as long as we are presenting code, this is my C# version of a fairly simple in-place permutation generator. Permutation generator It does not use recursion, but I did use the convenient yield return statement of C#. In place of the yield return you can call a program that uses the permutations. There are two axillary arrays, one that is the inverse of the permutation array and one that gives direction of movement.

The main loop has about 15 instructions. At the end of the permutation array, I give the values of the two columns that were exchanged. These two columns can be applied to any number of other permutations. I hope the documentation explains what is going on. Let me know if there are any questions.

Thanks for the suggestions.
@camertron and @LostInParadise, how do I go about running those codes. Sorry to be so illiterate but I don’t have any idea how to run those codes. :(

BonusQuestion (1482)

You can run my code by placing it on a webserver that has PHP installed, and you can run @LostInParadise‘s code by downloading Microsoft’s Visual Studio. That’s all pretty complicated though. What do you want this code to do? What’s your end goal?

I made the code runnable with some input boxes here. Hope that helps!

camertron (2102)

@camertron Thanks for taking the time to help me. I’d appreciate it.

My end goal is to check if the final numbers are divisible by a given number, and I do want the number of sets to vary.

BonusQuestion (1482)

Alright, well, what’s the largest number of sets you’d want to compute this on? It would probably be unwise to go beyond 10.

camertron (2102)

I don’t think it gets beyond 10. I think 7 would be enough for now. Thanks again.

BonusQuestion (1482)

Sure, I’ll see what I can do. It might have to wait until the weekend though :)

camertron (2102)

Thanks @camertron. A friend wrote me a code. I guess I am done with this for now. Thanks all.

BonusQuestion (1482)

or