(PC-12xx~14xx) pnxcb combination generator
|
03-31-2021, 12:41 AM
(This post was last modified: 03-31-2021 08:42 PM by robve.)
Post: #1
|
|||
|
|||
(PC-12xx~14xx) pnxcb combination generator
PNXCB COMBINATION GENERATOR
For SHARP PC-12xx to 14xx Credit: ALG. PNXCB https://dl.acm.org/doi/pdf/10.1145/355826.355830 258 bytes BASIC image (PC-1350) PNXCB(P,N,K,X,Y) cycles through array of "pointers" P(1..K) to N "items". - First initialize P to a list of K distinct "pointers" (indices) to N items. - Then call PNXCB \( n \choose k \) times to generate all combinations of K items. - If initially all P(I)=I, then \( n \choose k \) cycle is complete when P(K)=K. - Also returns X and Y updated "pointers" in P, which may be useful. Example: find Pythagorian triples up to 40: (x,y,z) such that x2 + y2 = z2 10 N=40,K=3: DIM P(K): P(1)=1,P(2)=2,P(3)=3 20 IF P(1)*P(1)+P(2)*P(2)=P(3)*P(3) PRINT P(1);P(2);P(3) 30 GOSUB "PNXCB" 40 IF P(K)<>K GOTO 20 50 END RUN 3. 4. 5. 6. 8. 10. 5. 12. 13. 9. 12. 15. 8. 15. 17. 12. 16. 20. 7. 24. 25. 15. 20. 25. 10. 24. 26. 20. 21. 29. 18. 24. 30. 16. 30. 34. 21. 28. 35. 12. 35. 37. 15. 36. 39. 24. 32. 40. Example: find all pairs of distinct prime numbers p and q up to 97 such that p + q = 100 10 N=25,K=2: DIM P(K),R(N): RESTORE 20 FOR I=1 TO K: P(I)=I: NEXT I 30 FOR I=1 TO N: READ R(I): NEXT I 40 DATA 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97 50 P=R(P(1)),Q=R(P(2)): IF P+Q=100 PRINT P,Q 60 GOSUB "PNXCB" 70 IF P(K)<>K GOTO 50 80 END RUN 3. 97. 11. 89. 17. 83. 29. 71. 41. 59. 47. 53. Code: ' PNXCB COMBINATION GENERATOR Edit: fixed typo + display example program output. "I count on old friends" -- HP 71B,Prime|Ti VOY200,Nspire CXII CAS|Casio fx-CG50...|Sharp PC-G850,E500,2500,1500,14xx,13xx,12xx... |
|||
« Next Oldest | Next Newest »
|
Messages In This Thread |
(PC-12xx~14xx) pnxcb combination generator - robve - 03-31-2021 12:41 AM
|
User(s) browsing this thread: 1 Guest(s)