Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

BOFH excuse #193: Did you pay the new Support Fee?


comp / comp.lang.lisp / Re: The "Strand" puzzle --- ( Continued Fractions using Lisp orPython? )

SubjectAuthor
* Re: The "Strand" puzzle --- ( Continued Fractions using Lisp or Python? )HenHanna
`* Re: The "Strand" puzzle --- ( Continued Fractions using Lisp orPython? )B. Pym
 +- Re: The "Strand" puzzle --- ( Continued Fractions using Lisp orPython? )Madhu
 +* Re: The "Strand" puzzle --- ( Continued Fractions using Lisp orPython? )HenHanna
 |+- Re: The "Strand" puzzle --- ( Continued Fractions using Lisp orPython? )Moebius
 |`* Re: The "Strand" puzzle --- ( Continued Fractions using Lisp orPython? )Madhu
 | `- Re: The "Strand" puzzle --- ( Continued Fractions using Lisp orPython? )HenHanna
 +* Re: The "Strand" puzzle --- ( Continued Fractions using Lisp orPython? )B. Pym
 |`- Re: The "Strand" puzzle --- ( Continued Fractions using Lisp orPython? )B. Pym
 `- Re: The "Strand" puzzle --- ( Continued Fractions using Lisp orPython? )B. Pym

1
Subject: Re: The "Strand" puzzle --- ( Continued Fractions using Lisp or Python? )
From: HenHanna
Newsgroups: rec.puzzles, sci.lang, sci.math, comp.lang.lisp, comp.lang.python
Organization: A noiseless patient Spider
Date: Mon, 29 Jul 2024 18:58 UTC
References: 1 2
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: HenHanna@devnull.tb (HenHanna)
Newsgroups: rec.puzzles,sci.lang,sci.math,comp.lang.lisp,comp.lang.python
Subject: Re: The "Strand" puzzle --- ( Continued Fractions using Lisp or
Python? )
Date: Mon, 29 Jul 2024 11:58:21 -0700
Organization: A noiseless patient Spider
Lines: 42
Message-ID: <v88ood$jnp6$1@dont-email.me>
References: <v7u7qd$2dgbs$1@dont-email.me>
<6f90c2b4abed28c153dea46de3af408d@www.novabbs.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 29 Jul 2024 20:58:22 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="cda7bbd820da97e0a9151b7b6d2c87f6";
logging-data="646950"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+1q4Hf2t50AvEH3WtlpAjaw5ASmsZJU9Y="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:ZoyBmh4ek+Wgc/jkRLEuhqe3t7A=
In-Reply-To: <6f90c2b4abed28c153dea46de3af408d@www.novabbs.com>
Content-Language: en-US
View all headers

On 7/26/2024 5:37 AM, IlanMayer wrote:
> On Thu, 25 Jul 2024 19:07:56 +0000, 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”
>>
>>
>>
>>   Ramanujan and Strand Puzzle
>>
>>             this was a very interesting puzzle tackled by the genius
>> Srinivasa Ramanujan.        In the year 1914, P.C. Mahalanobis, a Kings
>> college student in England, got hold of a puzzle from the Strand
>> magazine.
>
> Solution found at:
> https://ubpdqnmathematica.wordpress.com/2021/12/05/ramanujan-and-strand-puzzle/

thanks!

>>> So the solutions to the Strand puzzle can be found from the
continued fraction of \sqrt{2}, which _is_ satisfying simple.

>>> Using Mathematica to look at the first 10 convergents

---------- is this (also) easy to do using Lisp or Python???

Subject: Re: The "Strand" puzzle --- ( Continued Fractions using Lisp orPython? )
From: B. Pym
Newsgroups: rec.puzzles, sci.lang, sci.math, comp.lang.lisp, comp.lang.python
Followup: rec.puzzles
Organization: A noiseless patient Spider
Date: Thu, 1 Aug 2024 09:33 UTC
References: 1 2 3
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Nobody447095@here-nor-there.org (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: <v8fkps$23nr5$1@dont-email.me>
References: <v7u7qd$2dgbs$1@dont-email.me> <6f90c2b4abed28c153dea46de3af408d@www.novabbs.com> <v88ood$jnp6$1@dont-email.me>
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=
View all headers

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

Subject: Re: The "Strand" puzzle --- ( Continued Fractions using Lisp orPython? )
From: Madhu
Newsgroups: comp.lang.lisp
Organization: Motzarella
Date: Thu, 1 Aug 2024 18:11 UTC
References: 1 2 3 4
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: enometh@meer.net (Madhu)
Newsgroups: comp.lang.lisp
Subject: Re: The "Strand" puzzle --- ( Continued Fractions using Lisp orPython? )
Date: Thu, 01 Aug 2024 23:41:47 +0530
Organization: Motzarella
Lines: 153
Message-ID: <m3o76cru3g.fsf@leonis4.robolove.meer.net>
References: <v7u7qd$2dgbs$1@dont-email.me>
<6f90c2b4abed28c153dea46de3af408d@www.novabbs.com>
<v88ood$jnp6$1@dont-email.me> <v8fkps$23nr5$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 01 Aug 2024 20:11:58 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="279c53cc96f6474a1c520e3f53e85bee";
logging-data="2417945"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+4qntwQkmVgdAEO2UWrIGWa0VRYi4Q2Yw="
Cancel-Lock: sha1:bc3o+KcBv/jPC1FSljUrSLgfmXI=
sha1:zpovIaHHlVG+FG6+OKUUk3bnM74=
View all headers

* W J <v8fkps$23nr5$1@dont-email.me> :
Wrote on Thu, 1 Aug 2024 09:33:53 -0000 (UTC):

> 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"

No, You did not comprehend the problem correctly. The house numbers
(one ons side of the street) start from 1. there were at least 50 houses
and not more than 500 houses in that row. see below:

From the portion which WJ snipped out:

* In <6f90c2b4abed28c153dea46de3af408d@www.novabbs.com>
On 7/26/2024 5:37 AM, IlanMayer wrote:
> Solution found at:
> https://ubpdqnmathematica.wordpress.com/2021/12/05/ramanujan-and-strand-puzzle/

```
tesseract <(ls
ubpdqnmathematica.wordpress.com/wp-content/uploads/2021/12/puzzle.jpg)
- --dpi 150 txt
```

```
"I was talking the other day," said William Rogers
to the other villagers gathered round the inn fire,
"to a gentleman about that place called Louvain, what
the Germans have burnt down. He said he knowed it
well—used to visit a Belgian friend there. He said the
house of his friend was in a long street, numbered on
his side one, two, three, and so on, and that all the
numbers on one side of him added up exactly the same
as all the numbers on the other side of him. Funny
thing that! He said he knew there was more than
fifty houses on that side of the street, but not so many
as five hundred. I made mention of the matter to our
[parson]
and he took a pencil and worked out the number
Belgian lived. I don't know
[how he done it."]

Perhaps the reader may like to discover the number
‘of that house
```

English (ESL) Usage: Is "he knowed it" French?

> 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

No to answer the question you need to do (strand (lrange 1 500))

I'll go back to understand the mathematica solution a little later,
but in lisp using JMsiskind's screamer constraint solver

(require 'screamer)

(defun sum (a b)
"(loop for i from a to b sum i)"
(/ (- (+ (* b b) b) (- (* a a) a)) 2))

(defun balance (a b &key (constrain-left t) (constrain-right nil))
(screamer:all-values
(let* ((n (screamer:an-integer-between a b))
(l (if constrain-left a (screamer:an-integer-between a (1- n))))
(r (if constrain-right b (screamer:an-integer-between (1+ n) b))))
(screamer:assert! (= (sum l (1- n)) (sum (1+ n) r)))
(screamer:solution
(list l n r)
(screamer:static-ordering #'screamer:linear-force)))))

;; a list of (1st-house-number solution-house-number
;; last-house-number)

(balance 1 500)
;; => ((1 6 8) (1 35 49) (1 204 288))

This matches the numbers in the ubpdqnmathematica generated table.

The solution from ubpdqnmathematica equates:

(sum (- n l) (- l 1)) == (sum (+ l 1) r)

l = 1
r = number of houses
n = position of the required house

and expanding and substituting x = 2n + 1, y = 2r

eventually reduces that to the solutions of x^2 - 2y^ = 1 (a "Pell
equation"), for which apparently the solutions are to be found in the
convergents (num/den) of sqrt(2), I'm not sure how.

num = 2 * r + 1
den = 2 * n

;; sqrt(2) = 1 + 1
;; ==============
;; 2 + 1
;; ===========
;; 2 + 1
;; ========
;; 2 + 1
;; ======
;; 2 + ...

(defun nth-convergent-of-sqrt-2 (n)
(assert (>= n 0))
(let ((den 2))
(loop repeat n do (setq den (+ 2 (/ 1 den))))
(+ 1 (/ 1 den))))

(setq $a (loop for i below 10 collect (nth-convergent-of-sqrt-2 i)))

(loop for a in $a
when (let ((num (numerator a))
(den (denominator a)))
(when (= 1 (- (* num num) (* 2 den den)))
(list 1 ;house #1
(/ den 2); the house# in the middle
(/ (- num 1) 2); the last house no.
)))
collect it)

=> ((1 1 1) (1 8 6) (1 49 35) (1 288 204) (1 1681 1189))

Which reproduces the result but I've forgotten the maths behind the
continued fractions which I'm sure I was taught in either highschool or
1st year of college.

Subject: Re: The "Strand" puzzle --- ( Continued Fractions using Lisp orPython? )
From: HenHanna
Newsgroups: rec.puzzles, comp.lang.lisp, sci.lang, sci.math
Organization: A noiseless patient Spider
Date: Thu, 1 Aug 2024 19:47 UTC
References: 1 2 3 4
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: HenHanna@devnull.tb (HenHanna)
Newsgroups: rec.puzzles,comp.lang.lisp,sci.lang,sci.math
Subject: Re: The "Strand" puzzle --- ( Continued Fractions using Lisp
orPython? )
Date: Thu, 1 Aug 2024 12:47:30 -0700
Organization: A noiseless patient Spider
Lines: 63
Message-ID: <v8gook$2bqob$1@dont-email.me>
References: <v7u7qd$2dgbs$1@dont-email.me>
<6f90c2b4abed28c153dea46de3af408d@www.novabbs.com>
<v88ood$jnp6$1@dont-email.me> <v8fkps$23nr5$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 01 Aug 2024 21:47:33 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="9ea15a95b9ca9618e834905215ad3b29";
logging-data="2485003"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/deG8xSTqB2zj4naYAlVPYB1a47xgt/BE="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:0J/2dufs5EVqZ2Ph3EIPx4XfZ6A=
Content-Language: en-US
In-Reply-To: <v8fkps$23nr5$1@dont-email.me>
View all headers

On 8/1/2024 2:33 AM, B. Pym wrote:
> 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

does your Newsreader set Followup-to automatically?
pls disable it.

i still don't see how the prob is related to the Continued Fraction.

if this Continued Fraction is written as [2,1] with a bar over 1 (?)

What does [3,1] correspond to?

Subject: Re: The "Strand" puzzle --- ( Continued Fractions using Lisp orPython? )
From: Moebius
Newsgroups: rec.puzzles, comp.lang.lisp, sci.lang, sci.math
Organization: A noiseless patient Spider
Date: Thu, 1 Aug 2024 19:50 UTC
References: 1 2 3 4 5
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: invalid@example.invalid (Moebius)
Newsgroups: rec.puzzles,comp.lang.lisp,sci.lang,sci.math
Subject: Re: The "Strand" puzzle --- ( Continued Fractions using Lisp
orPython? )
Date: Thu, 1 Aug 2024 21:50:51 +0200
Organization: A noiseless patient Spider
Lines: 55
Message-ID: <v8gour$2b98k$1@dont-email.me>
References: <v7u7qd$2dgbs$1@dont-email.me>
<6f90c2b4abed28c153dea46de3af408d@www.novabbs.com>
<v88ood$jnp6$1@dont-email.me> <v8fkps$23nr5$1@dont-email.me>
<v8gook$2bqob$1@dont-email.me>
Reply-To: invalid@example.invalid
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 01 Aug 2024 21:50:51 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="d1357b6aef03946392c01cfb304e64ea";
logging-data="2467092"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/S9ucr71pxC6LH5lM/O2AM"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:qoQbrKo+IMQOEUZZUujqNn/Haog=
In-Reply-To: <v8gook$2bqob$1@dont-email.me>
Content-Language: de-DE
View all headers

Am 01.08.2024 um 21:47 schrieb HenHanna:
> On 8/1/2024 2:33 AM, B. Pym wrote:
>> 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
>
>
>               does your Newsreader  set  Followup-to automatically?
>                        pls  disable it.

https://vasya10.wordpress.com/2012/01/11/groovy-olc-2-strand-puzzle-1914/
> i still don't see how the prob is related to the Continued  Fraction.

Subject: Re: The "Strand" puzzle --- ( Continued Fractions using Lisp orPython? )
From: B. Pym
Newsgroups: comp.lang.lisp, comp.lang.scheme
Organization: A noiseless patient Spider
Date: Fri, 2 Aug 2024 12:47 UTC
References: 1 2 3 4
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Nobody447095@here-nor-there.org (B. Pym)
Newsgroups: comp.lang.lisp,comp.lang.scheme
Subject: Re: The "Strand" puzzle --- ( Continued Fractions using Lisp orPython? )
Date: Fri, 2 Aug 2024 12:47:00 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 58
Message-ID: <v8ikg2$2qrts$1@dont-email.me>
References: <v7u7qd$2dgbs$1@dont-email.me> <6f90c2b4abed28c153dea46de3af408d@www.novabbs.com> <v88ood$jnp6$1@dont-email.me> <v8fkps$23nr5$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 02 Aug 2024 14:47:00 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="7aeb7d3f9bdbec13555d482a35ea9d08";
logging-data="2977724"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1++9L3GbcNBzWu0oiqyfdoJ"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:iW6HGhiEGtOxM8g+HNk9TpX0bTs=
View all headers

B. Pym 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

Faster:

(define (strand lst)
(let go ((left-sum 0) (right-sum (fold + 0 (cdr lst))) (tail lst))
(cond ((< left-sum right-sum)
(go (+ left-sum (car tail))
(- right-sum (cadr tail))
(cdr tail)))
((= left-sum right-sum) (car tail))
(else #f))))

Subject: Re: The "Strand" puzzle --- ( Continued Fractions using Lisp orPython? )
From: B. Pym
Newsgroups: comp.lang.lisp, comp.lang.scheme
Organization: A noiseless patient Spider
Date: Sat, 3 Aug 2024 02:39 UTC
References: 1 2 3 4 5
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Nobody447095@here-nor-there.org (B. Pym)
Newsgroups: comp.lang.lisp,comp.lang.scheme
Subject: Re: The "Strand" puzzle --- ( Continued Fractions using Lisp orPython? )
Date: Sat, 3 Aug 2024 02:39:52 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 68
Message-ID: <v8k59k$382b5$1@dont-email.me>
References: <v7u7qd$2dgbs$1@dont-email.me> <6f90c2b4abed28c153dea46de3af408d@www.novabbs.com> <v88ood$jnp6$1@dont-email.me> <v8fkps$23nr5$1@dont-email.me> <v8ikg2$2qrts$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 03 Aug 2024 04:39:52 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="a358947f45c9b99589af96dcca686d10";
logging-data="3410277"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX199gXi9Br2gbfjCcexf/NKF"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:Zt7P8ANA+N4CnCChTJXH32k9O1c=
View all headers

B. Pym wrote:

> B. Pym 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
>
>
> Faster:
>
> (define (strand lst)
> (let go ((left-sum 0) (right-sum (fold + 0 (cdr lst))) (tail lst))
> (cond ((< left-sum right-sum)
> (go (+ left-sum (car tail))
> (- right-sum (cadr tail))
> (cdr tail)))
> ((= left-sum right-sum) (car tail))
>

Using "do":

(define (strand lst)
(do ((tail lst (cdr tail))
(left-sum 0 (+ left-sum (car tail)))
(right-sum (fold + 0 (cdr lst)) (- right-sum (cadr tail))))
((>= left-sum right-sum)
(if (= left-sum right-sum) (car tail) #f))))

Subject: Re: The "Strand" puzzle --- ( Continued Fractions using Lisp orPython? )
From: Madhu
Newsgroups: comp.lang.lisp
Organization: Motzarella
Date: Sat, 3 Aug 2024 15:49 UTC
References: 1 2 3 4 5
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: enometh@meer.net (Madhu)
Newsgroups: comp.lang.lisp
Subject: Re: The "Strand" puzzle --- ( Continued Fractions using Lisp orPython? )
Date: Sat, 03 Aug 2024 21:19:02 +0530
Organization: Motzarella
Lines: 95
Message-ID: <m3v80hr4i9.fsf@leonis4.robolove.meer.net>
References: <v7u7qd$2dgbs$1@dont-email.me>
<6f90c2b4abed28c153dea46de3af408d@www.novabbs.com>
<v88ood$jnp6$1@dont-email.me> <v8fkps$23nr5$1@dont-email.me>
<v8gook$2bqob$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Date: Sat, 03 Aug 2024 17:49:14 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="0b78c09d3bc8bf095c30520414183be9";
logging-data="3702780"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/ZfFfGjtqDcsAXvcsfdjixMpGPcB1xmFI="
Cancel-Lock: sha1:ARjaMtD+CEGl9LP5PaqmDZhUNtg=
sha1:KlnGrhEVYniecJ23frM06zRDjg8=
View all headers

* HenHanna <v8gook$2bqob$1@dont-email.me> :
Wrote on Thu, 1 Aug 2024 12:47:30 -0700:
> i still don't see how the prob is related to the Continued Fraction.
>
>
>
> if this Continued Fraction is written as [2,1] with a bar over 1 (?)

No it is written as [1:(2)], with the bar over 2 replaced by bracketing
the periodic terms.
It is the same as [1;2,2,2,2,2,]

I had found this page which explains the
sqrt (2) as a continued fraction -- when I posted my parallel answer in
<m3o76cru3g.fsf@leonis4.robolove.meer.net>

https://projecteuler.net/problem=65

and a clojure solution for it
https://raw.githubusercontent.com/rm-hull/project-euler/master/src/euler065.clj

which explains the convergents for sqrt(2)

; What does [3,1] correspond to?

[3;(1)]
another continued fraction with a list of peridoicity 1 consisting of
(1)

Here is a fucntion to compute the nth convergents of continued fractions
speciefied like this (first repeated-numbers)

(defun nth-convergent (n first repeated &aux (l (length repeated)))
(+ first (if (= n 0)
0
(let (den)
(loop for i downfrom n to 1
for k = (- i 1)
for e = (elt repeated (mod k l))
do (setq den (if den (+ e (/ den)) e)))
(if den (/ den) 0)))))

(nth-convergent 0 3 '(1))

(loop for i below 10 collect (nth-convergent i 3 '(1)))
=> (3 4 7/2 11/3 18/5 29/8 47/13 76/21 123/34 199/55)

=> (3.0 4.0 3.5 3.6666667 3.6 3.625 3.6153846 3.6190476 3.6176472 3.6181817)

which seems to converge to 3.618034,

punching the numerators into
https://oeis.org/

brings up this page https://oeis.org/A000032 for "Lucas Numbers
beginning with 2"

I see I can generalize the n-convergent above function to solve
project-euler-65 by letting it accept a "generator" function, (not your
usual generator more of an accessor which does table lookup)

(defun nth-convergent-acc (n acc)
"Produce the nth convergent of a continued fraction specified by
the nth-accessor ACC function."
(let (den)
(loop for i downfrom n to 1
for e = (funcall acc i)
do (setq den (if (not den) e (+ e (/ den)))))
(+ (funcall acc 0) (if den (/ den) 0))))

(defun sqrt2-acc (n) ;;[1;(2)]
(if (= n 0) 1 2))

(loop for i below 10 collect (nth-convergent-acc i #'sqrt2-acc))
;; => (1 3/2 7/5 17/12 41/29 99/70 239/169 577/408 1393/985 3363/2378)

(defun nth-acc-e (n) ;;[2;1,2,1,1,4,1,1,6,1,...] repeating (1,2k,1) A0034157
(if (= n 0)
2
(multiple-value-bind (div rem)
(truncate (1- n) 3)
(ecase rem
(0 1)
(1 (* 2 (1+ div)))
(2 1)))))

(defun nth-convergent-e (n)
(nth-convergent-acc n #'nth-acc-e))

(loop for i below 10 collect (nth-convergent-e i))
;; => (2 3 8/3 11/4 19/7 87/32 106/39 193/71 1264/465 1457/536)

Subject: Re: The "Strand" puzzle --- ( Continued Fractions using Lisp orPython? )
From: HenHanna
Newsgroups: comp.lang.lisp
Organization: A noiseless patient Spider
Date: Sat, 3 Aug 2024 17:46 UTC
References: 1 2 3 4 5 6
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: HenHanna@devnull.tb (HenHanna)
Newsgroups: comp.lang.lisp
Subject: Re: The "Strand" puzzle --- ( Continued Fractions using Lisp
orPython? )
Date: Sat, 3 Aug 2024 10:46:23 -0700
Organization: A noiseless patient Spider
Lines: 109
Message-ID: <v8lqdg$3hvbb$7@dont-email.me>
References: <v7u7qd$2dgbs$1@dont-email.me>
<6f90c2b4abed28c153dea46de3af408d@www.novabbs.com>
<v88ood$jnp6$1@dont-email.me> <v8fkps$23nr5$1@dont-email.me>
<v8gook$2bqob$1@dont-email.me> <m3v80hr4i9.fsf@leonis4.robolove.meer.net>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 03 Aug 2024 19:46:25 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="6f3bd7d8758ba97c90d294b030b071cc";
logging-data="3734891"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/XVAf3dsHGzgz9RJIOSuM5VqZJjXQfPdo="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:xflyHnFjjLUicA3X3ZoxXoHrlLE=
Content-Language: en-US
In-Reply-To: <m3v80hr4i9.fsf@leonis4.robolove.meer.net>
View all headers

On 8/3/2024 8:49 AM, Madhu wrote:
> * HenHanna <v8gook$2bqob$1@dont-email.me> :
> Wrote on Thu, 1 Aug 2024 12:47:30 -0700:
>> i still don't see how the prob is related to the Continued Fraction.
>>
>>
>>
>> if this Continued Fraction is written as [2,1] with a bar over 1 (?)
>
> No it is written as [1:(2)], with the bar over 2 replaced by bracketing
> the periodic terms.
> It is the same as [1;2,2,2,2,2,]
>
> I had found this page which explains the
> sqrt (2) as a continued fraction -- when I posted my parallel answer in
> <m3o76cru3g.fsf@leonis4.robolove.meer.net>
>
> https://projecteuler.net/problem=65
>
> and a clojure solution for it
> https://raw.githubusercontent.com/rm-hull/project-euler/master/src/euler065.clj
>
> which explains the convergents for sqrt(2)
>
>
>
>
> ; What does [3,1] correspond to?
>
> [3;(1)]
> another continued fraction with a list of peridoicity 1 consisting of
> (1)
>
> Here is a fucntion to compute the nth convergents of continued fractions
> speciefied like this (first repeated-numbers)
>
>
> (defun nth-convergent (n first repeated &aux (l (length repeated)))
> (+ first (if (= n 0)
> 0
> (let (den)
> (loop for i downfrom n to 1
> for k = (- i 1)
> for e = (elt repeated (mod k l))
> do (setq den (if den (+ e (/ den)) e)))
> (if den (/ den) 0)))))
>
> (nth-convergent 0 3 '(1))
>
> (loop for i below 10 collect (nth-convergent i 3 '(1)))
> => (3 4 7/2 11/3 18/5 29/8 47/13 76/21 123/34 199/55)
>
> => (3.0 4.0 3.5 3.6666667 3.6 3.625 3.6153846 3.6190476 3.6176472 3.6181817)
>
> which seems to converge to 3.618034,
>
> punching the numerators into
> https://oeis.org/
>
> brings up this page https://oeis.org/A000032 for "Lucas Numbers
> beginning with 2"
>
> I see I can generalize the n-convergent above function to solve
> project-euler-65 by letting it accept a "generator" function, (not your
> usual generator more of an accessor which does table lookup)
>
> (defun nth-convergent-acc (n acc)
> "Produce the nth convergent of a continued fraction specified by
> the nth-accessor ACC function."
> (let (den)
> (loop for i downfrom n to 1
> for e = (funcall acc i)
> do (setq den (if (not den) e (+ e (/ den)))))
> (+ (funcall acc 0) (if den (/ den) 0))))
>
> (defun sqrt2-acc (n) ;;[1;(2)]
> (if (= n 0) 1 2))
>
> (loop for i below 10 collect (nth-convergent-acc i #'sqrt2-acc))
> ;; => (1 3/2 7/5 17/12 41/29 99/70 239/169 577/408 1393/985 3363/2378)
>
> (defun nth-acc-e (n) ;;[2;1,2,1,1,4,1,1,6,1,...] repeating (1,2k,1) A0034157
> (if (= n 0)
> 2
> (multiple-value-bind (div rem)
> (truncate (1- n) 3)
> (ecase rem
> (0 1)
> (1 (* 2 (1+ div)))
> (2 1)))))
>
> (defun nth-convergent-e (n)
> (nth-convergent-acc n #'nth-acc-e))
>
> (loop for i below 10 collect (nth-convergent-e i))
> ;; => (2 3 8/3 11/4 19/7 87/32 106/39 193/71 1264/465 1457/536)

thanks... (i meant to ask...)

>> if this Continued-Fraction is written as [2,1] with a bar over 1 (?)

the Continued-Fraction for Sqrt(2) gives us the "Strand" puzzle---

Do the other simple Continued-Fractions give us
other similar problems ???

Subject: Re: The "Strand" puzzle --- ( Continued Fractions using Lisp orPython? )
From: B. Pym
Newsgroups: comp.lang.lisp
Organization: A noiseless patient Spider
Date: Sun, 4 Aug 2024 21:29 UTC
References: 1 2 3 4
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Nobody447095@here-nor-there.org (B. Pym)
Newsgroups: comp.lang.lisp
Subject: Re: The "Strand" puzzle --- ( Continued Fractions using Lisp orPython? )
Date: Sun, 4 Aug 2024 21:29:45 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 69
Message-ID: <v8ors5$8fen$1@dont-email.me>
References: <v7u7qd$2dgbs$1@dont-email.me> <6f90c2b4abed28c153dea46de3af408d@www.novabbs.com> <v88ood$jnp6$1@dont-email.me> <v8fkps$23nr5$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 04 Aug 2024 23:29:46 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="e14708043080897b7486957644b6603d";
logging-data="277975"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+Y+2y/ttBGO96oacjeN61G"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:8t/RDeCLCZIZt2NsfQq0w4Q9sVA=
View all headers

B. Pym wrote:

> 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

newLISP

(define (strand lst)
(let (left-sum 0
right-sum (apply + (rest lst) 2))
(while (< left-sum right-sum)
(++ left-sum (pop lst))
(-- right-sum (first lst)))
(and (= left-sum right-sum) (first lst))))

(sequence 2 4)
===>
(2 3 4)

(exists (fn (n) (strand (sequence 50 n))) (sequence 500 50))
===>
351

(strand (sequence 50 351))
===>
251

1

rocksolid light 0.9.8
clearnet tor