Under the numbers {1, 4, 8, 10, 20, 25, 50} there are three, the product is 100. What is the sum of these three numbers?
Let's define combination function to find all possible sets of three numbers and shortcut for product.
(define (combinations k nlst)
(cond ((zero? k)
'(()))
((null? nlst)
'())
(else
(append (map (lambda (k-1)
(cons (car nlst) k-1))
(combinations (- k 1) (cdr nlst)))
(combinations k (cdr nlst))))))
(define (product xlst)
(foldl * 1 xlst))
(filter (lambda(x) (= (product x) 100)) (combinations 3 '(1 4 8 10 20 25 50 )))
Answer is 30 and set is {1, 4, 25}
Enjoy!
No comments:
Post a Comment