• Re: The "Strand" puzzle --- ( Continued Fractions using Lisp orPython?

    From Madhu@21:1/5 to All on Sat Aug 3 21:19:02 2024
    * 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)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From HenHanna@21:1/5 to Madhu on Sat Aug 3 10:46:23 2024
    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 ???

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From B. Pym@21:1/5 to B. Pym on Sun Aug 4 21:29:45 2024
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From B. Pym@21:1/5 to HenHanna on Thu Aug 1 09:33:53 2024
    XPost: rec.puzzles, sci.lang, sci.math
    XPost: comp.lang.python

    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Madhu@21:1/5 to IlanMayer on Thu Aug 1 23:41:47 2024
    * 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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Moebius@21:1/5 to All on Thu Aug 1 21:50:51 2024
    XPost: rec.puzzles, sci.lang, sci.math

    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From HenHanna@21:1/5 to B. Pym on Thu Aug 1 12:47:30 2024
    XPost: rec.puzzles, sci.lang, sci.math

    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?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From B. Pym@21:1/5 to B. Pym on Fri Aug 2 12:47:00 2024
    XPost: comp.lang.scheme

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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From B. Pym@21:1/5 to B. Pym on Sat Aug 3 02:39:52 2024
    XPost: comp.lang.scheme

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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)