Paste number 46467: triples

Index of paste annotations: 1

Paste number 46467: triples
Pasted by: fax
When:1 year, 10 months ago
Share:Tweet this! | http://paste.lisp.org/+ZUR
Channel:#lispcafe
Paste contents:
Raw Source | XML | Display As
(defun sums-to-n (a b c n) (= (+ a b c) n))


(defun triples (n &aux triples)
  (loop for c from 3 to 10 do
    (loop for b from 2 to c do
      (loop for a from 1 to b
            if (sums-to-n a b c n)
              do (push (list a b c) triples))))
  triples)


(defun range (start end)
  (loop for i from start to end collect i))

(defun triples (n &aux triples)
  (dolist (a (range 3 10))
    (dolist (b (range 2 a))
      (dolist (c (range 1 b))
        (when (sums-to-n a b c n)
          (push (list a b c) triples))))) triples)



(defun triples (n)
  (mapcan #'(lambda (c)
              (mapcan #'(lambda (b)
                          (mapcan #'(lambda (a)
                                      (when (sums-to-n a b c n)
                                        (list (list a b c))))
                                  (range 1 b)))
                      (range 2 c)))
          (range 3 10)))

Annotations for this paste:

Annotation number 1: bar
Pasted by: foo
When:1 year, 10 months ago
Share:Tweet this! | http://paste.lisp.org/+ZUR#1
Paste contents:
Raw Source | Display As
(defun all-triples (n &optional (max 10))
  (iter @ (for i from 1 to max)
	(iter (for j from (1+ i) to max)
	      (iter (for k from (1+ j) to max)
		    (if (= (+ i j k) n)
			(in @ (collect (list i j k))))))))

Colorize as:
Show Line Numbers
Index of paste annotations: 1

Lisppaste pastes can be made by anyone at any time. Imagine a fearsomely comprehensive disclaimer of liability. Now fear, comprehensively.