Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Permutations of a number string

Status
Not open for further replies.

baseballgem

Programmer
Oct 3, 2001
54
US
I have the probelm of trying to get all the permuations of a string up to 9 characters.

EX) if its' "123" the result would be 3 factorial possibilities or 6.
123,132,231,213,321,312

For 8 "12345678" there are 8 factorial possibilities
I solved the problwm with arrays, for 8 factorial is 40320.

However, 9 factorial is 362880 too many elements for an array (program bombs).
Someone said just make a table for that case.

Oh well I suppose I couold; but I also recall seeing a long time ago (in BASIC) a very tight function that did this
using recursive calls.
My program is over 75 lines so I'm sure there's got to be a tighter cleaner way to do this.

Is anyone familiar with recursive calls or complete permutations of a string.
 
9 factorial is 362880 too many elements for an array

Well, that sounds like your problem is not how to calculate all the perms, but how to store the results. One solution would be to append the data to a cursor. Another would be simply to use the result as soon as you calculate it, and then discard it.

As to the method of calculating the perms, I won't try to offer you any code, but it does sound like a classic example of recursion. I'd bet any decent text book on numeric algorithms would cover it.

Mike


Mike Lewis
Edinburgh, Scotland
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top