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

Liste des GroupesRevenir à cl lisp 
Sujet : Re: The "Strand" puzzle --- ( Continued Fractions using Lisp orPython? )
De : enometh (at) *nospam* meer.net (Madhu)
Groupes : comp.lang.lisp
Date : 01. Aug 2024, 19:11:47
Autres entêtes
Organisation : Motzarella
Message-ID : <m3o76cru3g.fsf@leonis4.robolove.meer.net>
References : 1 2 3 4
* 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.


Date Sujet#  Auteur
29 Jul 24 * Re: The "Strand" puzzle --- ( Continued Fractions using Lisp or Python? )10HenHanna
1 Aug 24 `* Re: The "Strand" puzzle --- ( Continued Fractions using Lisp orPython? )9B. Pym
1 Aug 24  +- Re: The "Strand" puzzle --- ( Continued Fractions using Lisp orPython? )1Madhu
1 Aug 24  +* Re: The "Strand" puzzle --- ( Continued Fractions using Lisp orPython? )4HenHanna
1 Aug 24  i+- Re: The "Strand" puzzle --- ( Continued Fractions using Lisp orPython? )1Moebius
3 Aug 24  i`* Re: The "Strand" puzzle --- ( Continued Fractions using Lisp orPython? )2Madhu
3 Aug 24  i `- Re: The "Strand" puzzle --- ( Continued Fractions using Lisp orPython? )1HenHanna
2 Aug 24  +* Re: The "Strand" puzzle --- ( Continued Fractions using Lisp orPython? )2B. Pym
3 Aug 24  i`- Re: The "Strand" puzzle --- ( Continued Fractions using Lisp orPython? )1B. Pym
4 Aug 24  `- Re: The "Strand" puzzle --- ( Continued Fractions using Lisp orPython? )1B. Pym

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal