All combinations of 3numbers out of 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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | * Get all combinations of 3 numbers out of 9. * Raynald Levesque 2000/11/24. SET MPRINT=yes. DATA LIST FREE /nbers. BEGIN DATA 1 END DATA. LIST. * This macro finds all combinations of 3 numbers out of n (where n > 3). *////////////////////////////////. DEFINE !doit (!POS=!CMDEND) !LET !x=!NULL STRING answer1 TO answer3 (A8). !DO !var !IN (!1) !LET !x=!CONCAT(!x,!BLANKS(1)) STRING !CONCAT('V',!LENGTH(!x)) (A8). COMPUTE !CONCAT('V',!LENGTH(!x))=!QUOTE(!var). !DOEND !LET !lastv1=!LENGTH(!SUBSTR(!x,3)) !LET !lastv2=!LENGTH(!SUBSTR(!x,2)) !LET !lastv3=!LENGTH(!SUBSTR(!x,1)) VECTOR vec=v1 TO !CONCAT('V',!LENGTH(!x)). LOOP #cnt1=1 TO !lastv1. COMPUTE answer1=vec(#cnt1). LOOP #cnt2=#cnt1 + 1 TO !lastv2. COMPUTE answer2=vec(#cnt2). LOOP #cnt3=#cnt2 + 1 TO !lastv3. COMPUTE answer3=vec(#cnt3). COMPUTE answer=NUMBER(CONCAT(RTRIM(answer1),RTRIM(answer2),RTRIM(answer3)),F8.0). XSAVE OUTFILE='c:\\temp\\temp.sav' /KEEP=answer. END LOOP. END LOOP. END LOOP. EXECUTE. GET FILE='c:\\temp\\temp.sav'. FORMATS answer (F8.0). !ENDDEFINE. *////////////////////////////////. !doit 18 5 95 11 8 76 14 4 79. **************@@@@@@@@@ Alternative method. * (unknown author). new file. input program. loop #i1=1 to 9-2. loop #i2=#i1+1 to 9-1. loop #i3=#i2+1 to 9. compute v1=#i1. compute v2=#i2. compute v3=#i3. leave v1 v2. end case. end loop. end loop. end loop. end file. end input program. execute. recode v1,v2,v3 (1=18)(2=5)(3=95)(4=11)(5=8)(6=76)(7=14)(8=4)(9=79). compute answer=number(concat(ltrim(string(v1,F8.0)),ltrim(string(v2,F8.0)),ltrim(string(v3,F8.0))),F8.0). execute. formats answer (F8.0). |
Related pages
...