Subj : Re: how to generate a sequence? To : comp.programming From : gerard46 Date : Fri Sep 09 2005 03:46 am | Neo wrote: | Hi All, | I have problem, I want to generate all possible (unique) combinations of the | following sequence: | | 123456789 | 213456789 | 312456789 | 321456789 | . | . | Symbols allowed : 1 - 9 | Though, symbols can be anything like A B C D E F G H I, not necessarily | numbers. | NOTE: there will be !n (Factorial n) lines of output, 'coz there can be !n | combinations of n symbols. | Algo. required! Do U have a quick solution in mind??? Neo: here is a program that generates all possible PERMUATATIONs of your sequence. Almost half the code is dealing with a user-specified set of symbols (which may be more than one character). Program input: x = number of things y = number (taken-at-a-time) between = character(s) used between symbols (may be null) usyms = list of symbols to be used, they default to: 1 2 3 4 5 6 7 8 9 A B C ... Y Z a b c ... Y Z 63 64 65 ... To make the program easier to read, no error checking is done to see if: X or Y not being postive integers, Y not being less or equal to X. The basic method is to count in base X starting with the X metadrome number --- see METADROME numbers [see INDEX] as defined in the On-line Encylopedia of Integer Sequences [OEIS]: http://www.research.att.com/~njas/sequences/index.html _________________________________________________________________________ /*rexx*/ parse arg x y between usyms /*take X things Y at a time.*/ @abc='abcdefghijklmnopqrstuvwxyz' @abcu=@abc; upper @abcu @abcs=@abc||@abcu @0abcs=123456789||@abcs syms.= /*placeholder for symbols. */ sep= do k=1 to x /*build list of symbols. */ _=p(word(usyms,k) p(substr(@0abcs,k,1) k)) /*get or generate a symbol. */ if length(_)\==1 then sep='_' /*if not 1char, then use sep*/ syms.k=_ /*append to the sumbol list.*/ end if between=='' then between=sep /*use appropriate seperator.*/ /*------------------------------------for metadrome #s, see OEIS.-------+ | 1 2 3 4 5 6 7 8 9 10 11 | | - - - -- --- ----- ------ ------- --------- ----------- -------------| | 0 1 5 27 194 1,865 22,875 342,391 6,053,444 123,456,789 2,853,116,705| +----------------------------------------------------------------------*/ do j=metadrome(y) for permutation(x,y) n=j z.= /*placeholders for "digits".*/ do k=1 for y /*convert N to base X. */ _=n//x+1 /*add 1 to each "digit". */ n=n%x /*note: % is integer divide.*/ z.k=syms._ /*put "digit" into an array.*/ end _=z.y /*use first "digit". */ do k=y-1 to 1 by -1 /*build the permuation "num"*/ _=_||between||z.k end say _ end exit metadrome: procedure; arg p _=1; m=0; do k=p-1 to 1 by -1; m=m+k*_; _=_*p; end; return m permutation: procedure; arg x,y; _=1; do j=x-y+1 to x; _=_*j; end; return _ p: return word(arg(1),1) ____________________________________________________________________Gerard S. .