Find all permutations of integers 1 to n
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | * This macro finds all permutations of the integers 1, 2, ... t where t < 8. * Raynald Levesque Feb. 2002. DEFINE !t(nb=!TOKENS(1)) /* Create a file containing all combinations (with replacements)*/. INPUT PROGRAM. !DO !cnt=1 !TO !nb LOOP !CONCAT('cnt',!cnt,'=1 TO ',!nb). LEAVE !CONCAT('cnt',!cnt). !DOEND END CASE. !DO !cnt=1 !TO !nb. END LOOP. !DOEND END FILE. END INPUT PROGRAM. * Count if some number appears more than once. NUMERIC c1 TO !CONCAT('c',!nb). !DO !cnt=1 !TO !nb COUNT !CONCAT('c',!cnt,' =cnt1 TO c',!cnt,'(',!cnt,')'). !DOEND /* Keep only true permutations (without replacements)*/. SELECT IF MAX(c1 TO !CONCAT('c',!nb))=1. EXECUTE. STRING newname(A8). COMPUTE newname=CONCAT('p',LTRIM(STRING($CASENUM,F8.0))). FLIP cnt1 TO !CONCAT('cnt',!nb) /NEWNAMES=newname. SAVE OUTFILE='c:\\temp\\all permutations.sav' /DROP=case_lbl. !ENDDEFINE. !t nb=6. |
Related pages
...