Path: ...!3.eu.feeder.erje.net!feeder.erje.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: "B. Pym" Newsgroups: rec.puzzles,sci.lang,sci.math,comp.lang.lisp,comp.lang.python Subject: Re: The "Strand" puzzle --- ( Continued Fractions using Lisp orPython? ) Followup-To: rec.puzzles Date: Thu, 1 Aug 2024 09:33:53 -0000 (UTC) Organization: A noiseless patient Spider Lines: 45 Message-ID: References: <6f90c2b4abed28c153dea46de3af408d@www.novabbs.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Date: Thu, 01 Aug 2024 11:33:53 +0200 (CEST) Injection-Info: dont-email.me; posting-host="5e130b75a0a049f5f5ae85e7f8f6d757"; logging-data="2219877"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19cxGN4XMqLZZFP0GT+ekBb" User-Agent: XanaNews/1.18.1.6 Cancel-Lock: sha1:EcElLL6qNDDBuCFI2BHvpl8UPvo= Bytes: 2086 HenHanna wrote: > > > e.g. -------- For the (street)  Numbers (1,2,3,4,5,6,7,8) > > > > > >        (1,2,3,4,5)  and  (7,8)  both add up to 15. > > > > > > > > > > > > "In a given street of houses with consecutive numbers between > > > 50 and 500, find the house number, for which, the sum of > > > numbers on the left is equal to the sum of numbers on the > > > right" Gauche Scheme (define (strand lst) (let go ((left-sum 0) (tail lst)) (if (null? tail) #f (let ((right-sum (fold + 0 (cdr tail)))) (cond ((< left-sum right-sum) (go (+ left-sum (car tail)) (cdr tail))) ((= left-sum right-sum) (car tail)) (#t #f)))))) (strand '(1 2 3 4 5 6 7 8)) ===> 6 (lrange 2 5) ===> (2 3 4) (any (lambda (n) (if (strand (lrange 50 n)) n #f)) (lrange 500 50 -1)) ===> 352 (strand (lrange 50 352)) ===> 251