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

    From B. Pym@21:1/5 to B. Pym on Fri Aug 2 12:06:09 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))


    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 Fri Aug 2 12:32:32 2024
    B. Pym wrote:

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

    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 Doc O'Leary ,@21:1/5 to B. Pym on Fri Aug 2 15:54:42 2024
    For your reference, records indicate that
    "B. Pym" <Nobody447095@here-nor-there.org> wrote:

    ===>
    352


    Faster:

    But is it correct? Because surrounding 352 I get (with Ruby):

    irb(main):001:0> (50..351).sum
    60551
    irb(main):002:0> (353..500).sum
    63122

    This solution for the example:

    x = 1
    1
    y = 8
    8
    (x..y).select {|n| (x..(n-1)).sum == ((n+1)..y).sum}
    [6]

    For the puzzle yields:

    x = 50
    50
    y = 500
    500
    (x..y).select {|n| (x..n-1).sum == (n+1..y).sum}
    []

    So no house. I’ll skip the full output, but for the tipping point I see:

    (x..y).select {|n| puts "#{n} #{(x..(n-1)).sum} #{((n+1)..y).sum}"}

    355 61610 62060
    356 61965 61704

    --
    "Also . . . I can kill you with my brain."
    River Tam, Trash, Firefly

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Doc O'Leary ,@21:1/5 to droleary@2017usenet1.subsume.com on Fri Aug 2 16:55:06 2024
    For your reference, records indicate that
    Doc O'Leary , <droleary@2017usenet1.subsume.com> wrote:

    Hmmm. Not sure why it was using my old email. Anyway!

    So no house. I’ll skip the full output, but for the tipping point I see:

    (x..y).select {|n| puts "#{n} #{(x..(n-1)).sum} #{((n+1)..y).sum}"}

    355 61610 62060
    356 61965 61704

    I went a step further and figured out the equation to solve the puzzle. Implemented in Ruby:

    range_sum_midpoint(1,8)
    6.0
    range_sum_midpoint(50,500)
    355.6332380416656

    The equation itself is left as an exercise for the rec.puzzles reader. :-)

    --
    "Also . . . I can kill you with my brain."
    River Tam, Trash, Firefly

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