Discussion:
X in every language syndrome
(too old to reply)
B. Pym
2024-07-07 11:48:28 UTC
Permalink
The question is about so-called "bellied"
numbers, defined as 4-digit integers for which the sum of the two
"middle" digits is smaller than the sum of the two outer digits. So 1265
is bellied, while 4247 is not.
[ He means the sum of middle digits is larger. ]
(defun bellied-number-p (n)
(> (+ (mod (truncate n 10) 10) (mod (truncate n 100) 10))
(+ (mod n 10) (truncate n 1000))))
Now the task is to find the longest, uninterrupted sequence of bellied
numbers within all 4-digit number, hence from 1000 to 9999. And this is
While the following code does the job,
(let ((max-length 0)
(current-length 0)
(last-bellied-number 0))
(dotimes (m 9000)
(let ((n (+ 1000 m)))
(if (bellied-number-p n)
(incf current-length)
(progn
(when (> current-length max-length)
(setf max-length current-length)
(setf last-bellied-number (1- n)))
(setf current-length 0)))))
(print (format t "~&Longest sequence of ~a bellied numbers ends at ~a."
max-length last-bellied-number)))
[ Another poster: ]
TXR Lisp.
(defun bellied-p (num)
(let ((d (digits num)))
(and (= 4 (len d))
(< (+ [d 0] [d 3])
(+ [d 1] [d 2])))))
[iff [chain car bellied-p] len (ret 0)]]
(1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932
1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945
1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958
1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971
1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984
1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997
1998 1999)
Shorter.

Gauche Scheme:

(use gauche.sequence) ;; group-contiguous-sequence find-max
,print-mode pretty #t length #f width 64

(define (bellied? n)
(define (d i) (mod (div n (expt 10 i)) 10))
(> (+ (d 1) (d 2))
(+ (d 0) (d 3))))

(find-max
(group-contiguous-sequence (filter bellied? (iota 9000 1000)))
:key length)

===>
(1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931
1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943
1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955
1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967
1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979
1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991
1992 1993 1994 1995 1996 1997 1998 1999)


In Forth?
Ahmed
2024-07-07 14:36:39 UTC
Permalink
Hi,


include clp_v2.fs

\ abcd
\ a, b, c, d in {0,..,9} in general
\ abcd is between 1920 and 2000 in this example

\ b+c > a+d

0 to min_val
10 to max_val

0 value a
0 value b
0 value c
0 value d

: solve
_begin_
2 1 .---> a a a?,
10 9 .---> b b a?,
10 2 .---> c c a?,
10 0 .---> d d a?,

b c + a d + > t?,

a 10 * b + 10 * c + 10 * d + .
_end_
;


solve gvies:

solve 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932
1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946
1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960
1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974
1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988
1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 ok

Here I used clp_v2.fs given hereafter


\ ----- for CLP
0 value vals_num
20 value vals_num_max

0 value min_val
0 value max_val

0 value nloops_prec
0 value nloops
0 value constraint_num
30 value max_num_constraints
0 value constraint_count

: values dup 1+ to vals_num 0 ?do 0 value loop ;
: fvalues 0 ?do 0e fvalue loop ;

create loop_loc max_num_constraints allot
loop_loc max_num_constraints erase

create constraints_stack max_num_constraints cells allot
constraints_stack max_num_constraints cells erase

constraints_stack value constraints_stack_pointer

: push_to_constraints_stack
constraints_stack_pointer cell+ to constraints_stack_pointer
constraints_stack_pointer !
;
: pop_from_constraints_stack
constraints_stack_pointer dup @
swap cell- to constraints_stack_pointer
;

: update_constraints
constraint_num 1+ dup to constraint_count to constraint_num
nloops nloops_prec <> if
1 loop_loc constraint_num + c!
nloops to nloops_prec
then
;
: resolve_constraints
loop_loc constraint_num + c@ if
postpone loop
then
constraint_num 1- to constraint_num
;
: .---> nloops 1+ to nloops postpone do postpone i postpone to ;
immediate
: ----> postpone to ; immediate
: a| postpone then resolve_constraints ; immediate
: t| postpone then resolve_constraints ; immediate
: a?,
postpone min_val postpone max_val postpone 1+ postpone within
postpone if
update_constraints ['] a| push_to_constraints_stack
; immediate
: t?,
postpone if
update_constraints ['] t| push_to_constraints_stack
; immediate
: _begin_ ; immediate
: _end_
constraint_count 0 do
pop_from_constraints_stack execute
loop
; immediate

\ ---------end for CLP
Ahmed
2024-07-07 14:59:50 UTC
Permalink
Here, another version


include clp_v2.fs

\ abcd
\ a, b, c, d in {0,..,9}, here abcd between 1920 and 2000
\ b+c > a+d
0 to min_val
10000 to max_val

5 values abcd a b c d

: solve
_begin_
2000 1920 .---> abcd abcd a?,
abcd 10 /mod
10 /mod
10 /mod
to d to c to b to a
b c + a d + > t?,

abcd .
_end_
;
mhx
2024-07-07 16:24:47 UTC
Permalink
Why would I want to do this in Lisp? (nice CLP example though):

FORTH> : solve
<3>[FORTH>] 0 0 #10 LOCALS| cr? n4+n1 n3+n2 |
<3>[FORTH>] CR #2000 #1920
<3>[FORTH>] DO
[3]<3>[FORTH>] I #10 /MOD SWAP TO n4+n1
[3]<3>[FORTH>] #10 /MOD SWAP TO n3+n2
[3]<3>[FORTH>] #10 /MOD SWAP +TO n3+n2
[3]<3>[FORTH>] +TO n4+n1
[3]<3>[FORTH>] n3+n2 n4+n1 > IF I . 1 -TO cr? ENDIF
[3]<3>[FORTH>] cr? 0= IF CR #10 TO cr? ENDIF
[3]<3>[FORTH>] LOOP ; ok
FORTH> solve
1920 1921 1922 1923 1924 1925 1926 1927 1928 1929
1930 1931 1932 1933 1934 1935 1936 1937 1938 1939
1940 1941 1942 1943 1944 1945 1946 1947 1948 1949
1950 1951 1952 1953 1954 1955 1956 1957 1958 1959
1960 1961 1962 1963 1964 1965 1966 1967 1968 1969
1970 1971 1972 1973 1974 1975 1976 1977 1978 1979
1980 1981 1982 1983 1984 1985 1986 1987 1988 1989
1990 1991 1992 1993 1994 1995 1996 1997 1998 1999
ok

-marcel
B. Pym
2024-07-07 19:40:26 UTC
Permalink
Post by B. Pym
The question is about so-called "bellied"
numbers, defined as 4-digit integers for which the sum of the two
"middle" digits is smaller than the sum of the two outer digits. So 1265
is bellied, while 4247 is not.
[ He means the sum of middle digits is larger. ]
(defun bellied-number-p (n)
(> (+ (mod (truncate n 10) 10) (mod (truncate n 100) 10))
(+ (mod n 10) (truncate n 1000))))
Now the task is to find the longest, uninterrupted sequence of bellied
numbers within all 4-digit number, hence from 1000 to 9999. And this is
While the following code does the job,
(let ((max-length 0)
(current-length 0)
(last-bellied-number 0))
(dotimes (m 9000)
(let ((n (+ 1000 m)))
(if (bellied-number-p n)
(incf current-length)
(progn
(when (> current-length max-length)
(setf max-length current-length)
(setf last-bellied-number (1- n)))
(setf current-length 0)))))
(print (format t "~&Longest sequence of ~a bellied numbers ends at ~a."
max-length last-bellied-number)))
[ Another poster: ]
TXR Lisp.
(defun bellied-p (num)
(let ((d (digits num)))
(and (= 4 (len d))
(< (+ [d 0] [d 3])
(+ [d 1] [d 2])))))
[iff [chain car bellied-p] len (ret 0)]]
(1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932
1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945
1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958
1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971
1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984
1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997
1998 1999)
Shorter.
(use gauche.sequence) ;; group-contiguous-sequence find-max
,print-mode pretty #t length #f width 64
(define (bellied? n)
(define (d i) (mod (div n (expt 10 i)) 10))
(> (+ (d 1) (d 2))
(+ (d 0) (d 3))))
(find-max
(group-contiguous-sequence (filter bellied? (iota 9000 1000)))
:key length)
===>
(1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931
1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943
1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955
1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967
1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979
1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991
1992 1993 1994 1995 1996 1997 1998 1999)
In Forth?
A lower-level way.

(define (bellied? n)
(define (d i) (mod (div n (expt 10 i)) 10))
(> (+ (d 1) (d 2))
(+ (d 0) (d 3))))

(define (calc-length i)
(do ((j i (+ j 1)))
((not (bellied? j)) (- j i))))

(define (longest-bellied-seq)
(let go ((i 1000) (start 0) (len 0))
(if (> i 9999)
(list start len)
(let ((new-len (calc-length i)))
(cond ((zero? new-len) (go (+ i 1) start len))
((> new-len len) (go (+ i new-len) i new-len))
(#t (go (+ i new-len) start len)))))))

===>
(1920 80)

Loading...