Jump to content

Featured Replies

  • Author

I would actually need ALL possibilities …

(defun comb-to-sum (n)
  (let ((nums (gen-integer 1 n))
        (x 0))
    (find-unique
     (sort-asc
      (flatten-sublist
       (loop repeat n
         for i = (combination2 (incf x) nums)
         collect (loop for c in i
                   if (= (sum c) n)
                   collect c)))))))
                   
(comb-to-sum 3)
=> ((3) (1 2) (1 1 1))

(comb-to-sum 5)
=> ((5) (1 4) (2 3) (1 1 3) (1 2 2) (1 1 1 2) (1 1 1 1 1))

 

If you need the function in the OM system I could add it with the next update.

In what context you are using this combination?

  • Author

I use this to select different unique modules for Live-Electronics.

Here ist my adapted function:

 

(defun comb-to-sum (n &key (reps t))
  (let* ((nums (gen-integer 1 n))
         (x 0)
         (result
          (find-unique
           (sort-asc
            (flatten-sublist
             (loop repeat n
               for i = (combination2 (incf x) nums)
               collect (loop for c in i
                         if (= (sum c) n)
                         collect c)))))))
    (if reps
      result
      (loop for i in result
        when (equal (sum (remove-duplicates i)) n)
        collect i))))

(comb-to-sum 5 :reps nil) ; => ((5) (1 4) (2 3))

 

Create an account or sign in to comment


Copyright © 2014-2025 Opusmodus™ Ltd. All rights reserved.
Product features, specifications, system requirements and availability are subject to change without notice.
Opusmodus, the Opusmodus logo, and other Opusmodus trademarks are either registered trademarks or trademarks of Opusmodus Ltd.
All other trademarks contained herein are the property of their respective owners.

Powered by Invision Community

Important Information

Terms of Use Privacy Policy