Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Successful and fortunate crime is called virtue. -- Seneca


devel / comp.lang.forth / SEND+MORE=MONEY

SubjectAuthor
* SEND+MORE=MONEYminf...@arcor.de
+* Re: SEND+MORE=MONEYJali Heinonen
|`* Re: SEND+MORE=MONEYJali Heinonen
| `* Re: SEND+MORE=MONEYminf...@arcor.de
|  `- Re: SEND+MORE=MONEYGerry Jackson
+- Re: SEND+MORE=MONEYMarcel Hendrix
+* Re: SEND+MORE=MONEYMarcel Hendrix
|`* Re: SEND+MORE=MONEYminf...@arcor.de
| +* Re: SEND+MORE=MONEYMarcel Hendrix
| |`- Re: SEND+MORE=MONEYminf...@arcor.de
| `* Re: SEND+MORE=MONEYAnton Ertl
|  `* Re: SEND+MORE=MONEYminf...@arcor.de
|   `- Re: SEND+MORE=MONEYminf...@arcor.de
+* Re: SEND+MORE=MONEYAnton Ertl
|+- Re: SEND+MORE=MONEYAnton Ertl
|`* Re: SEND+MORE=MONEYMarcel Hendrix
| `- Re: SEND+MORE=MONEYAnton Ertl
+* Re: SEND+MORE=MONEYPaul Rubin
|`* Re: SEND+MORE=MONEYAnton Ertl
| +* Re: SEND+MORE=MONEYJali Heinonen
| |+- Re: SEND+MORE=MONEYAnton Ertl
| |`- Re: SEND+MORE=MONEYPaul Rubin
| +* Re: SEND+MORE=MONEYminf...@arcor.de
| |`* Re: SEND+MORE=MONEYAnton Ertl
| | +- locals slowness in sf and vfx (was: SEND+MORE=MONEY)Anton Ertl
| | `* Re: SEND+MORE=MONEYnone
| |  +* Re: SEND+MORE=MONEYminf...@arcor.de
| |  |`* Re: SEND+MORE=MONEYAla'a
| |  | `* Re: SEND+MORE=MONEYMarcel Hendrix
| |  |  `* Re: SEND+MORE=MONEYAla'a
| |  |   `* Re: SEND+MORE=MONEYMarcel Hendrix
| |  |    +* Re: SEND+MORE=MONEYAla'a
| |  |    |`* Re: SEND+MORE=MONEYMarcel Hendrix
| |  |    | `* Re: SEND+MORE=MONEYMarcel Hendrix
| |  |    |  `* Re: SEND+MORE=MONEYAla'a
| |  |    |   `- Re: SEND+MORE=MONEYminf...@arcor.de
| |  |    `- Re: SEND+MORE=MONEYdxforth
| |  `- Re: SEND+MORE=MONEYAnton Ertl
| `* Re: SEND+MORE=MONEYPaul Rubin
|  `* Re: SEND+MORE=MONEYPaul Rubin
|   +* Re: SEND+MORE=MONEYminf...@arcor.de
|   |`- Re: SEND+MORE=MONEYPaul Rubin
|   +- Re: SEND+MORE=MONEYPaul Rubin
|   `* Re: SEND+MORE=MONEYAnton Ertl
|    +- Re: SEND+MORE=MONEYPaul Rubin
|    `* Re: SEND+MORE=MONEYPaul Rubin
|     `* Re: SEND+MORE=MONEYminf...@arcor.de
|      `* Re: SEND+MORE=MONEYGerry Jackson
|       +* Re: SEND+MORE=MONEYPaul Rubin
|       |+- Re: SEND+MORE=MONEYdxforth
|       |`- Re: SEND+MORE=MONEYminf...@arcor.de
|       `* Re: SEND+MORE=MONEYMarcel Hendrix
|        `* Re: SEND+MORE=MONEYminf...@arcor.de
|         +* Re: SEND+MORE=MONEYMarcel Hendrix
|         |`* Re: SEND+MORE=MONEYdxforth
|         | `- Re: SEND+MORE=MONEYMarcel Hendrix
|         `* Re: SEND+MORE=MONEYminf...@arcor.de
|          +* Re: SEND+MORE=MONEYMarcel Hendrix
|          |`* Re: SEND+MORE=MONEYHans Bezemer
|          | +* Re: SEND+MORE=MONEYminf...@arcor.de
|          | |`- Re: SEND+MORE=MONEYHans Bezemer
|          | +* Re: SEND+MORE=MONEYMarcel Hendrix
|          | |`- Re: SEND+MORE=MONEYdxforth
|          | `* Re: SEND+MORE=MONEYMarcel Hendrix
|          |  `* Re: SEND+MORE=MONEYHans Bezemer
|          |   `* Re: SEND+MORE=MONEYMarcel Hendrix
|          |    `* Re: SEND+MORE=MONEYHans Bezemer
|          |     `- Re: SEND+MORE=MONEYBrian Fox
|          `* Re: SEND+MORE=MONEYdxforth
|           +* Re: SEND+MORE=MONEYminf...@arcor.de
|           |`* Re: SEND+MORE=MONEYMarcel Hendrix
|           | +* Re: SEND+MORE=MONEYminf...@arcor.de
|           | |+- Re: SEND+MORE=MONEYMarcel Hendrix
|           | |`* Re: SEND+MORE=MONEYAnton Ertl
|           | | +* Re: SEND+MORE=MONEYminf...@arcor.de
|           | | |`- Re: SEND+MORE=MONEYHans Bezemer
|           | | `* Re: SEND+MORE=MONEYminf...@arcor.de
|           | |  `* Re: SEND+MORE=MONEYAnton Ertl
|           | |   `- Re: SEND+MORE=MONEYminf...@arcor.de
|           | +- Re: SEND+MORE=MONEYMarcel Hendrix
|           | `* Re: SEND+MORE=MONEYHans Bezemer
|           |  `* Re: SEND+MORE=MONEYMarcel Hendrix
|           |   +* Re: SEND+MORE=MONEYHans Bezemer
|           |   |`* Re: SEND+MORE=MONEYMarcel Hendrix
|           |   | `* Re: SEND+MORE=MONEYAnton Ertl
|           |   |  `- Re: SEND+MORE=MONEYMarcel Hendrix
|           |   `* Re: SEND+MORE=MONEYminf...@arcor.de
|           |    `- Re: SEND+MORE=MONEYMarcel Hendrix
|           `- Re: SEND+MORE=MONEYdxforth
+* Re: SEND+MORE=MONEYAhmed MELAHI
|`* Re: SEND+MORE=MONEYminf...@arcor.de
| `* Re: SEND+MORE=MONEYAhmed MELAHI
|  +* Re: SEND+MORE=MONEYAhmed MELAHI
|  |`- Re: SEND+MORE=MONEYAhmed MELAHI
|  `* Re: SEND+MORE=MONEYminf...@arcor.de
|   +- Re: SEND+MORE=MONEYJali Heinonen
|   +* Re: SEND+MORE=MONEYAhmed MELAHI
|   |`- Re: SEND+MORE=MONEYAhmed MELAHI
|   +* Re: SEND+MORE=MONEYAnton Ertl
|   |`* Re: SEND+MORE=MONEYminf...@arcor.de
|   | `- Re: SEND+MORE=MONEYAnton Ertl
|   +- Re: SEND+MORE=MONEYAhmed MELAHI
|   `* Re: SEND+MORE=MONEYAhmed MELAHI
`- Re: SEND+MORE=MONEYNN

Pages:12345
SEND+MORE=MONEY

<78cfcd0a-5bc4-4584-8a92-9e8005a0e306n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=22269&group=comp.lang.forth#22269

  copy link   Newsgroups: comp.lang.forth
X-Received: by 2002:ac8:7d0a:0:b0:3ba:18f5:b639 with SMTP id g10-20020ac87d0a000000b003ba18f5b639mr537603qtb.292.1675794846903;
Tue, 07 Feb 2023 10:34:06 -0800 (PST)
X-Received: by 2002:a0d:f205:0:b0:526:6089:3b70 with SMTP id
b5-20020a0df205000000b0052660893b70mr432539ywf.245.1675794846599; Tue, 07 Feb
2023 10:34:06 -0800 (PST)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.forth
Date: Tue, 7 Feb 2023 10:34:06 -0800 (PST)
Injection-Info: google-groups.googlegroups.com; posting-host=2003:f7:1f2e:e929:b501:264a:2a5b:86f1;
posting-account=AqNUYgoAAADmkK2pN-RKms8sww57W0Iw
NNTP-Posting-Host: 2003:f7:1f2e:e929:b501:264a:2a5b:86f1
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <78cfcd0a-5bc4-4584-8a92-9e8005a0e306n@googlegroups.com>
Subject: SEND+MORE=MONEY
From: minfo...@arcor.de (minf...@arcor.de)
Injection-Date: Tue, 07 Feb 2023 18:34:06 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 3005
 by: minf...@arcor.de - Tue, 7 Feb 2023 18:34 UTC

Today I doodled with constraint logic programming in Forth.

A classic beginner's example is the SEND+MORE=MONEY puzzlee,
where each letter stands for a digit in the range of 0 to 9
and which when concatenated represent a decimal number.
Constraint: all digits must be different.

I came up with the little program below using brute force.
It does its job, but ugly. Any ideas for improvement and acceleration?
Or syntax-wise? (Prolog does it so much more nicely).

\ ##### SENDMORE.FTH #####
: ALLDIFFERENT {: a b c d e f g h -- flag :}
false
a b = IF exit THEN
a c = IF exit THEN
a d = IF exit THEN
a e = IF exit THEN
a f = IF exit THEN
a g = IF exit THEN
a h = IF exit THEN
b c = IF exit THEN
b d = IF exit THEN
b e = IF exit THEN
b f = IF exit THEN
b g = IF exit THEN
b h = IF exit THEN
c d = IF exit THEN
c e = IF exit THEN
c f = IF exit THEN
c g = IF exit THEN
c h = IF exit THEN
d e = IF exit THEN
d f = IF exit THEN
d g = IF exit THEN
d h = IF exit THEN
e f = IF exit THEN
e g = IF exit THEN
e h = IF exit THEN
f g = IF exit THEN
f h = IF exit THEN
g h = IF exit THEN
drop true ;

: SENDMOREMONEY {: | s e n d m o r y s1 s2 s3 ct -- :}
0 to ct
1 9 DO i to s
1 9 DO i to m
1 9 DO i to e
1 9 DO i to d
0 9 DO i to n
0 9 DO i to o
0 9 DO i to r
0 9 DO i to y
s e n d m o r y alldifferent
IF
ct 1+ to ct
s 1000 * e 100 * + n 10 * + d + to s1
m 1000 * o 100 * + r 10 * + e + to s2
m 10000 * o 1000 * + n 100 * + e 10 * + y + to s3
s1 s2 + s3 =
IF
cr ." S=" s . ." E=" e . ." N=" n . ." D=" d .
cr ." M=" m . ." O=" o . ." R=" r . ." Y=" y .
cr ." " s1 . cr ." +" s2 . cr ." -----" cr ." " s3 .
THEN
THEN
-1 +LOOP -1 +LOOP -1 +LOOP -1 +LOOP -1 +LOOP -1 +LOOP -1 +LOOP -1 +LOOP
." loops:" ct . ;

SENDMOREMONEY

Re: SEND+MORE=MONEY

<2262b425-bea8-42ed-af45-7ecebea71fefn@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=22270&group=comp.lang.forth#22270

  copy link   Newsgroups: comp.lang.forth
X-Received: by 2002:ac8:5a0b:0:b0:3ab:6bd6:d3b5 with SMTP id n11-20020ac85a0b000000b003ab6bd6d3b5mr603462qta.80.1675797355312;
Tue, 07 Feb 2023 11:15:55 -0800 (PST)
X-Received: by 2002:a05:6902:50c:b0:864:7a63:938 with SMTP id
x12-20020a056902050c00b008647a630938mr652501ybs.104.1675797354959; Tue, 07
Feb 2023 11:15:54 -0800 (PST)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.forth
Date: Tue, 7 Feb 2023 11:15:54 -0800 (PST)
In-Reply-To: <78cfcd0a-5bc4-4584-8a92-9e8005a0e306n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=87.95.64.138; posting-account=kiOBZQoAAADFsAs31ZHaefxTuQxv84Wm
NNTP-Posting-Host: 87.95.64.138
References: <78cfcd0a-5bc4-4584-8a92-9e8005a0e306n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <2262b425-bea8-42ed-af45-7ecebea71fefn@googlegroups.com>
Subject: Re: SEND+MORE=MONEY
From: jali.hei...@gmail.com (Jali Heinonen)
Injection-Date: Tue, 07 Feb 2023 19:15:55 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 4669
 by: Jali Heinonen - Tue, 7 Feb 2023 19:15 UTC

tiistai 7. helmikuuta 2023 klo 20.34.08 UTC+2 minf...@arcor.de kirjoitti:
> Today I doodled with constraint logic programming in Forth.
>
> A classic beginner's example is the SEND+MORE=MONEY puzzlee,
> where each letter stands for a digit in the range of 0 to 9
> and which when concatenated represent a decimal number.
> Constraint: all digits must be different.
>
> I came up with the little program below using brute force.
> It does its job, but ugly. Any ideas for improvement and acceleration?
> Or syntax-wise? (Prolog does it so much more nicely).
>
> \ ##### SENDMORE.FTH #####
> : ALLDIFFERENT {: a b c d e f g h -- flag :}
> false
> a b = IF exit THEN
> a c = IF exit THEN
> a d = IF exit THEN
> a e = IF exit THEN
> a f = IF exit THEN
> a g = IF exit THEN
> a h = IF exit THEN
> b c = IF exit THEN
> b d = IF exit THEN
> b e = IF exit THEN
> b f = IF exit THEN
> b g = IF exit THEN
> b h = IF exit THEN
> c d = IF exit THEN
> c e = IF exit THEN
> c f = IF exit THEN
> c g = IF exit THEN
> c h = IF exit THEN
> d e = IF exit THEN
> d f = IF exit THEN
> d g = IF exit THEN
> d h = IF exit THEN
> e f = IF exit THEN
> e g = IF exit THEN
> e h = IF exit THEN
> f g = IF exit THEN
> f h = IF exit THEN
> g h = IF exit THEN
> drop true ;
>
> : SENDMOREMONEY {: | s e n d m o r y s1 s2 s3 ct -- :}
> 0 to ct
> 1 9 DO i to s
> 1 9 DO i to m
> 1 9 DO i to e
> 1 9 DO i to d
> 0 9 DO i to n
> 0 9 DO i to o
> 0 9 DO i to r
> 0 9 DO i to y
> s e n d m o r y alldifferent
> IF
> ct 1+ to ct
> s 1000 * e 100 * + n 10 * + d + to s1
> m 1000 * o 100 * + r 10 * + e + to s2
> m 10000 * o 1000 * + n 100 * + e 10 * + y + to s3
> s1 s2 + s3 =
> IF
> cr ." S=" s . ." E=" e . ." N=" n . ." D=" d .
> cr ." M=" m . ." O=" o . ." R=" r . ." Y=" y .
> cr ." " s1 . cr ." +" s2 . cr ." -----" cr ." " s3 .
> THEN
> THEN
> -1 +LOOP -1 +LOOP -1 +LOOP -1 +LOOP -1 +LOOP -1 +LOOP -1 +LOOP -1 +LOOP
> ." loops:" ct . ;
>
> SENDMOREMONEY

I have solved this one using 8th, mainly as a test for building permutations and letting the eval do the job:

----------------------------------

needs string/translate

private

: generate \ a n --
a:new ( 0 a:push ) 2 pick times -rot
over 4 pick w:exec
0
repeat
dup 2 pick n:< if
3 pick over a:_@ over n:< if
dup 2 n:mod !if
2 pick 0 2 pick a:<> drop
else
2 pick 4 pick 2 pick a:_@ 2 pick a:<> drop
then
2 pick 5 pick w:exec
3 pick over a:@ n:1+ 2 pick swap a:! drop
drop 0
else
3 pick over 0 a:! drop
n:1+
then
else
break
then
again 3drop drop ;

public
\ Note: callback word receives array reference
: a:permutations \ a w --
swap a:len #p:generate ;

\ Now, try solving alphametics...

"SEND MORE + MONEY =" constant alphametics
alphametics /[A-Z]/ r:/ ' s:cmp a:sort ' s:= a:uniq "" a:join constant unique-chars

: any-leading-zeros?
/\b[0]/ r:match nip ;

: app:main
"0123456789" null s:/
( 0 8 a:slice "" a:join
alphametics unique-chars rot s:translate dup any-leading-zeros? !if
dup eval if
. cr break
else
drop
then
else
drop
then ) a:permutations ;

----------------------------------
Running it gives:

root@DietPi:~# /opt/8th/bin/rpi64/8th permute.8th
9567 1085 + 10652 =
root@DietPi:~#

Re: SEND+MORE=MONEY

<29e76075-21c0-4ab8-b2e7-cd180ab4e959n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=22271&group=comp.lang.forth#22271

  copy link   Newsgroups: comp.lang.forth
X-Received: by 2002:ac8:584f:0:b0:3b6:3a58:911a with SMTP id h15-20020ac8584f000000b003b63a58911amr560382qth.350.1675797743184;
Tue, 07 Feb 2023 11:22:23 -0800 (PST)
X-Received: by 2002:a25:ec0a:0:b0:875:1c0e:981a with SMTP id
j10-20020a25ec0a000000b008751c0e981amr462550ybh.530.1675797742869; Tue, 07
Feb 2023 11:22:22 -0800 (PST)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.forth
Date: Tue, 7 Feb 2023 11:22:22 -0800 (PST)
In-Reply-To: <2262b425-bea8-42ed-af45-7ecebea71fefn@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=87.95.64.138; posting-account=kiOBZQoAAADFsAs31ZHaefxTuQxv84Wm
NNTP-Posting-Host: 87.95.64.138
References: <78cfcd0a-5bc4-4584-8a92-9e8005a0e306n@googlegroups.com> <2262b425-bea8-42ed-af45-7ecebea71fefn@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <29e76075-21c0-4ab8-b2e7-cd180ab4e959n@googlegroups.com>
Subject: Re: SEND+MORE=MONEY
From: jali.hei...@gmail.com (Jali Heinonen)
Injection-Date: Tue, 07 Feb 2023 19:22:23 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 4963
 by: Jali Heinonen - Tue, 7 Feb 2023 19:22 UTC

tiistai 7. helmikuuta 2023 klo 21.15.56 UTC+2 Jali Heinonen kirjoitti:
> tiistai 7. helmikuuta 2023 klo 20.34.08 UTC+2 minf...@arcor.de kirjoitti:
> > Today I doodled with constraint logic programming in Forth.
> >
> > A classic beginner's example is the SEND+MORE=MONEY puzzlee,
> > where each letter stands for a digit in the range of 0 to 9
> > and which when concatenated represent a decimal number.
> > Constraint: all digits must be different.
> >
> > I came up with the little program below using brute force.
> > It does its job, but ugly. Any ideas for improvement and acceleration?
> > Or syntax-wise? (Prolog does it so much more nicely).
> >
> > \ ##### SENDMORE.FTH #####
> > : ALLDIFFERENT {: a b c d e f g h -- flag :}
> > false
> > a b = IF exit THEN
> > a c = IF exit THEN
> > a d = IF exit THEN
> > a e = IF exit THEN
> > a f = IF exit THEN
> > a g = IF exit THEN
> > a h = IF exit THEN
> > b c = IF exit THEN
> > b d = IF exit THEN
> > b e = IF exit THEN
> > b f = IF exit THEN
> > b g = IF exit THEN
> > b h = IF exit THEN
> > c d = IF exit THEN
> > c e = IF exit THEN
> > c f = IF exit THEN
> > c g = IF exit THEN
> > c h = IF exit THEN
> > d e = IF exit THEN
> > d f = IF exit THEN
> > d g = IF exit THEN
> > d h = IF exit THEN
> > e f = IF exit THEN
> > e g = IF exit THEN
> > e h = IF exit THEN
> > f g = IF exit THEN
> > f h = IF exit THEN
> > g h = IF exit THEN
> > drop true ;
> >
> > : SENDMOREMONEY {: | s e n d m o r y s1 s2 s3 ct -- :}
> > 0 to ct
> > 1 9 DO i to s
> > 1 9 DO i to m
> > 1 9 DO i to e
> > 1 9 DO i to d
> > 0 9 DO i to n
> > 0 9 DO i to o
> > 0 9 DO i to r
> > 0 9 DO i to y
> > s e n d m o r y alldifferent
> > IF
> > ct 1+ to ct
> > s 1000 * e 100 * + n 10 * + d + to s1
> > m 1000 * o 100 * + r 10 * + e + to s2
> > m 10000 * o 1000 * + n 100 * + e 10 * + y + to s3
> > s1 s2 + s3 =
> > IF
> > cr ." S=" s . ." E=" e . ." N=" n . ." D=" d .
> > cr ." M=" m . ." O=" o . ." R=" r . ." Y=" y .
> > cr ." " s1 . cr ." +" s2 . cr ." -----" cr ." " s3 .
> > THEN
> > THEN
> > -1 +LOOP -1 +LOOP -1 +LOOP -1 +LOOP -1 +LOOP -1 +LOOP -1 +LOOP -1 +LOOP
> > ." loops:" ct . ;
> >
> > SENDMOREMONEY
> I have solved this one using 8th, mainly as a test for building permutations and letting the eval do the job:
>
> ----------------------------------
>
> needs string/translate
>
> private
>
> : generate \ a n --
> a:new ( 0 a:push ) 2 pick times -rot
> over 4 pick w:exec
> 0
> repeat
> dup 2 pick n:< if
> 3 pick over a:_@ over n:< if
> dup 2 n:mod !if
> 2 pick 0 2 pick a:<> drop
> else
> 2 pick 4 pick 2 pick a:_@ 2 pick a:<> drop
> then
> 2 pick 5 pick w:exec
> 3 pick over a:@ n:1+ 2 pick swap a:! drop
> drop 0
> else
> 3 pick over 0 a:! drop
> n:1+
> then
> else
> break
> then
> again 3drop drop ;
>
> public
>
> \ Note: callback word receives array reference
> : a:permutations \ a w --
> swap a:len #p:generate ;
>
> \ Now, try solving alphametics...
>
> "SEND MORE + MONEY =" constant alphametics
> alphametics /[A-Z]/ r:/ ' s:cmp a:sort ' s:= a:uniq "" a:join constant unique-chars
>
> : any-leading-zeros?
> /\b[0]/ r:match nip ;
>
> : app:main
> "0123456789" null s:/
> ( 0 8 a:slice "" a:join
> alphametics unique-chars rot s:translate dup any-leading-zeros? !if
> dup eval if
> . cr break
> else
> drop
> then
> else
> drop
> then ) a:permutations ;
>
> ----------------------------------
> Running it gives:
>
> root@DietPi:~# /opt/8th/bin/rpi64/8th permute.8th
> 9567 1085 + 10652 =
> root@DietPi:~#
Sorry about losing indentations and making code hard to read....

Re: SEND+MORE=MONEY

<ed516b00-e861-4468-a7fb-4dc125773f7dn@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=22272&group=comp.lang.forth#22272

  copy link   Newsgroups: comp.lang.forth
X-Received: by 2002:a37:aa91:0:b0:71d:bbfe:6af0 with SMTP id t139-20020a37aa91000000b0071dbbfe6af0mr271452qke.327.1675800015722;
Tue, 07 Feb 2023 12:00:15 -0800 (PST)
X-Received: by 2002:a81:a541:0:b0:4fe:db73:d9c6 with SMTP id
v1-20020a81a541000000b004fedb73d9c6mr363303ywg.308.1675800015505; Tue, 07 Feb
2023 12:00:15 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border-2.nntp.ord.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.forth
Date: Tue, 7 Feb 2023 12:00:15 -0800 (PST)
In-Reply-To: <29e76075-21c0-4ab8-b2e7-cd180ab4e959n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=2003:f7:1f2e:e929:b501:264a:2a5b:86f1;
posting-account=AqNUYgoAAADmkK2pN-RKms8sww57W0Iw
NNTP-Posting-Host: 2003:f7:1f2e:e929:b501:264a:2a5b:86f1
References: <78cfcd0a-5bc4-4584-8a92-9e8005a0e306n@googlegroups.com>
<2262b425-bea8-42ed-af45-7ecebea71fefn@googlegroups.com> <29e76075-21c0-4ab8-b2e7-cd180ab4e959n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <ed516b00-e861-4468-a7fb-4dc125773f7dn@googlegroups.com>
Subject: Re: SEND+MORE=MONEY
From: minfo...@arcor.de (minf...@arcor.de)
Injection-Date: Tue, 07 Feb 2023 20:00:15 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 143
 by: minf...@arcor.de - Tue, 7 Feb 2023 20:00 UTC

Jali Heinonen schrieb am Dienstag, 7. Februar 2023 um 20:22:24 UTC+1:
> tiistai 7. helmikuuta 2023 klo 21.15.56 UTC+2 Jali Heinonen kirjoitti:
> > tiistai 7. helmikuuta 2023 klo 20.34.08 UTC+2 minf...@arcor.de kirjoitti:
> > > Today I doodled with constraint logic programming in Forth.
> > >
> > > A classic beginner's example is the SEND+MORE=MONEY puzzlee,
> > > where each letter stands for a digit in the range of 0 to 9
> > > and which when concatenated represent a decimal number.
> > > Constraint: all digits must be different.
> > >
> > > I came up with the little program below using brute force.
> > > It does its job, but ugly. Any ideas for improvement and acceleration?
> > > Or syntax-wise? (Prolog does it so much more nicely).
> > >
> > > \ ##### SENDMORE.FTH #####
> > > : ALLDIFFERENT {: a b c d e f g h -- flag :}
> > > false
> > > a b = IF exit THEN
> > > a c = IF exit THEN
> > > a d = IF exit THEN
> > > a e = IF exit THEN
> > > a f = IF exit THEN
> > > a g = IF exit THEN
> > > a h = IF exit THEN
> > > b c = IF exit THEN
> > > b d = IF exit THEN
> > > b e = IF exit THEN
> > > b f = IF exit THEN
> > > b g = IF exit THEN
> > > b h = IF exit THEN
> > > c d = IF exit THEN
> > > c e = IF exit THEN
> > > c f = IF exit THEN
> > > c g = IF exit THEN
> > > c h = IF exit THEN
> > > d e = IF exit THEN
> > > d f = IF exit THEN
> > > d g = IF exit THEN
> > > d h = IF exit THEN
> > > e f = IF exit THEN
> > > e g = IF exit THEN
> > > e h = IF exit THEN
> > > f g = IF exit THEN
> > > f h = IF exit THEN
> > > g h = IF exit THEN
> > > drop true ;
> > >
> > > : SENDMOREMONEY {: | s e n d m o r y s1 s2 s3 ct -- :}
> > > 0 to ct
> > > 1 9 DO i to s
> > > 1 9 DO i to m
> > > 1 9 DO i to e
> > > 1 9 DO i to d
> > > 0 9 DO i to n
> > > 0 9 DO i to o
> > > 0 9 DO i to r
> > > 0 9 DO i to y
> > > s e n d m o r y alldifferent
> > > IF
> > > ct 1+ to ct
> > > s 1000 * e 100 * + n 10 * + d + to s1
> > > m 1000 * o 100 * + r 10 * + e + to s2
> > > m 10000 * o 1000 * + n 100 * + e 10 * + y + to s3
> > > s1 s2 + s3 =
> > > IF
> > > cr ." S=" s . ." E=" e . ." N=" n . ." D=" d .
> > > cr ." M=" m . ." O=" o . ." R=" r . ." Y=" y .
> > > cr ." " s1 . cr ." +" s2 . cr ." -----" cr ." " s3 .
> > > THEN
> > > THEN
> > > -1 +LOOP -1 +LOOP -1 +LOOP -1 +LOOP -1 +LOOP -1 +LOOP -1 +LOOP -1 +LOOP
> > > ." loops:" ct . ;
> > >
> > > SENDMOREMONEY
> > I have solved this one using 8th, mainly as a test for building permutations and letting the eval do the job:
> >
> > ----------------------------------
> >
> > needs string/translate
> >
> > private
> >
> > : generate \ a n --
> > a:new ( 0 a:push ) 2 pick times -rot
> > over 4 pick w:exec
> > 0
> > repeat
> > dup 2 pick n:< if
> > 3 pick over a:_@ over n:< if
> > dup 2 n:mod !if
> > 2 pick 0 2 pick a:<> drop
> > else
> > 2 pick 4 pick 2 pick a:_@ 2 pick a:<> drop
> > then
> > 2 pick 5 pick w:exec
> > 3 pick over a:@ n:1+ 2 pick swap a:! drop
> > drop 0
> > else
> > 3 pick over 0 a:! drop
> > n:1+
> > then
> > else
> > break
> > then
> > again 3drop drop ;
> >
> > public
> >
> > \ Note: callback word receives array reference
> > : a:permutations \ a w --
> > swap a:len #p:generate ;
> >
> > \ Now, try solving alphametics...
> >
> > "SEND MORE + MONEY =" constant alphametics
> > alphametics /[A-Z]/ r:/ ' s:cmp a:sort ' s:= a:uniq "" a:join constant unique-chars
> >
> > : any-leading-zeros?
> > /\b[0]/ r:match nip ;
> >
> > : app:main
> > "0123456789" null s:/
> > ( 0 8 a:slice "" a:join
> > alphametics unique-chars rot s:translate dup any-leading-zeros? !if
> > dup eval if
> > . cr break
> > else
> > drop
> > then
> > else
> > drop
> > then ) a:permutations ;
> >
> > ----------------------------------
> > Running it gives:
> >
> > root@DietPi:~# /opt/8th/bin/rpi64/8th permute.8th
> > 9567 1085 + 10652 =
> > root@DietPi:~#
> Sorry about losing indentations and making code hard to read....

Thank you! Permutations could narrow the search space significantly indeed.
I have to think about how to implement them in standard Forth in order to replace
those eight nested DO..LOOPs with one single permutation loop.

Re: SEND+MORE=MONEY

<609c8969-40fa-4ae2-b9b1-cf7ab1d0671en@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=22279&group=comp.lang.forth#22279

  copy link   Newsgroups: comp.lang.forth
X-Received: by 2002:ac8:7c56:0:b0:3b9:c212:c49a with SMTP id o22-20020ac87c56000000b003b9c212c49amr1424932qtv.258.1675847730805;
Wed, 08 Feb 2023 01:15:30 -0800 (PST)
X-Received: by 2002:a5b:b43:0:b0:86e:c769:e975 with SMTP id
b3-20020a5b0b43000000b0086ec769e975mr794621ybr.81.1675847730430; Wed, 08 Feb
2023 01:15:30 -0800 (PST)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.forth
Date: Wed, 8 Feb 2023 01:15:30 -0800 (PST)
In-Reply-To: <78cfcd0a-5bc4-4584-8a92-9e8005a0e306n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=131.155.124.133; posting-account=-JQ2RQoAAAB6B5tcBTSdvOqrD1HpT_Rk
NNTP-Posting-Host: 131.155.124.133
References: <78cfcd0a-5bc4-4584-8a92-9e8005a0e306n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <609c8969-40fa-4ae2-b9b1-cf7ab1d0671en@googlegroups.com>
Subject: Re: SEND+MORE=MONEY
From: mhx...@iae.nl (Marcel Hendrix)
Injection-Date: Wed, 08 Feb 2023 09:15:30 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 1950
 by: Marcel Hendrix - Wed, 8 Feb 2023 09:15 UTC

On Tuesday, February 7, 2023 at 7:34:08 PM UTC+1, minf...@arcor.de wrote:
> Today I doodled with constraint logic programming in Forth.
[..]
> SENDMOREMONEY

It is unclear what you want to do?

Is it:
1. Given 8 numbers randomly drawn from the set { 0 .. 9 }, find all concatenations that do not start
with '0', and where all 8 numbers are different?

2. Is it to test if a particular set of 8 single-digit decimal numbers form one of the valid combinations?

Assuming it is 2, then:

Fill an array a with 0, 1, ... 9
Inspect the 8 numbers sequentially with index i
if the first number is '0', stop with result FALSE.
if a[i] = -1, stop with result FALSE ( we saw this number before )
store -1 in a[i] ( seen this number )
Stop with result TRUE.

This will also work
-marcel

Re: SEND+MORE=MONEY

<bd72798b-c201-4267-a2ff-2621482d4650n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=22280&group=comp.lang.forth#22280

  copy link   Newsgroups: comp.lang.forth
X-Received: by 2002:a05:620a:126d:b0:724:31b6:632a with SMTP id b13-20020a05620a126d00b0072431b6632amr375113qkl.432.1675848194464;
Wed, 08 Feb 2023 01:23:14 -0800 (PST)
X-Received: by 2002:a81:8842:0:b0:510:b7af:7e7c with SMTP id
y63-20020a818842000000b00510b7af7e7cmr563951ywf.70.1675848194197; Wed, 08 Feb
2023 01:23:14 -0800 (PST)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.forth
Date: Wed, 8 Feb 2023 01:23:13 -0800 (PST)
In-Reply-To: <78cfcd0a-5bc4-4584-8a92-9e8005a0e306n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=131.155.124.133; posting-account=-JQ2RQoAAAB6B5tcBTSdvOqrD1HpT_Rk
NNTP-Posting-Host: 131.155.124.133
References: <78cfcd0a-5bc4-4584-8a92-9e8005a0e306n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <bd72798b-c201-4267-a2ff-2621482d4650n@googlegroups.com>
Subject: Re: SEND+MORE=MONEY
From: mhx...@iae.nl (Marcel Hendrix)
Injection-Date: Wed, 08 Feb 2023 09:23:14 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 1905
 by: Marcel Hendrix - Wed, 8 Feb 2023 09:23 UTC

On Tuesday, February 7, 2023 at 7:34:08 PM UTC+1, minf...@arcor.de wrote:
> Today I doodled with constraint logic programming in Forth.
[..]
> SENDMOREMONEY

It is unclear what you want to do?

Is it:
1. Given 8 numbers randomly drawn from the set { 0 .. 9 }, find all concatenations that do not start
with '0', and where all 8 numbers are different?

2. Is it to test if a particular set of 8 single-digit decimal numbers form one of the valid
combinations?

Assuming it is 2, then:

Fill an array a with 0, 1, ... 9
Inspect the 8 numbers sequentially with index i
if the first number is '0', stop with result FALSE.
if a[i] = -1, stop with result FALSE ( we saw this number before )
store -1 in a[i] ( seen this number )
Stop with result TRUE.

-marcel

Re: SEND+MORE=MONEY

<trvpoc$3sti$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=22281&group=comp.lang.forth#22281

  copy link   Newsgroups: comp.lang.forth
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: do-not-...@swldwa.uk (Gerry Jackson)
Newsgroups: comp.lang.forth
Subject: Re: SEND+MORE=MONEY
Date: Wed, 8 Feb 2023 09:24:28 +0000
Organization: A noiseless patient Spider
Lines: 13
Message-ID: <trvpoc$3sti$1@dont-email.me>
References: <78cfcd0a-5bc4-4584-8a92-9e8005a0e306n@googlegroups.com>
<2262b425-bea8-42ed-af45-7ecebea71fefn@googlegroups.com>
<29e76075-21c0-4ab8-b2e7-cd180ab4e959n@googlegroups.com>
<ed516b00-e861-4468-a7fb-4dc125773f7dn@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 8 Feb 2023 09:24:28 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="c2556188bad964aa0738d9ef6b61f1d9";
logging-data="127922"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18z0ayaPjT0oWKtxgIbvAzn4mOGidlAAwE="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.6.1
Cancel-Lock: sha1:zXSj4wjD0QrkDKSmvJcrsNNrsY8=
In-Reply-To: <ed516b00-e861-4468-a7fb-4dc125773f7dn@googlegroups.com>
 by: Gerry Jackson - Wed, 8 Feb 2023 09:24 UTC

On 07/02/2023 20:00, minf...@arcor.de wrote:
> Jali Heinonen schrieb am Dienstag, 7. Februar 2023 um 20:22:24 UTC+1:
>
> Thank you! Permutations could narrow the search space significantly indeed.
> I have to think about how to implement them in standard Forth in order to replace
> those eight nested DO..LOOPs with one single permutation loop.

There's Heaps algorithm and an implementation of it at
https://groups.google.com/g/comp.lang.forth/c/xZO_hScBJiI/m/BapQC8g0DQAJ

--
Gerry

Re: SEND+MORE=MONEY

<2023Feb8.093829@mips.complang.tuwien.ac.at>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=22282&group=comp.lang.forth#22282

  copy link   Newsgroups: comp.lang.forth
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: ant...@mips.complang.tuwien.ac.at (Anton Ertl)
Newsgroups: comp.lang.forth
Subject: Re: SEND+MORE=MONEY
Date: Wed, 08 Feb 2023 08:38:29 GMT
Organization: Institut fuer Computersprachen, Technische Universitaet Wien
Lines: 93
Message-ID: <2023Feb8.093829@mips.complang.tuwien.ac.at>
References: <78cfcd0a-5bc4-4584-8a92-9e8005a0e306n@googlegroups.com>
Injection-Info: reader01.eternal-september.org; posting-host="51bbd3e42738c50cd53c2860b5745e5a";
logging-data="154876"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+OIlRcFtUchE+7q43Gm9HA"
Cancel-Lock: sha1:N0gaNAOt/T9v2/oZxy63YbG0DNw=
X-newsreader: xrn 10.11
 by: Anton Ertl - Wed, 8 Feb 2023 08:38 UTC

"minf...@arcor.de" <minforth@arcor.de> writes:
>I came up with the little program below using brute force.
>It does its job, but ugly. Any ideas for improvement and acceleration?
>Or syntax-wise? (Prolog does it so much more nicely).

For performance:

Have a map of already-occupied digits, and only scan through those
that are still free; that eliminates ALLDIFFERENT.

You could start with the last digits, and compute the digits of MONEY
from the others rather than scanning it.

I wonder if BacForth (from Michael Gassanenko, supports backtracking)
would be of advantage here, but I suspect not.

Writing about it, I get inspired to try it myself:

create occupationmap 10 allot
\ each entry is 0 if free, non-0 if occupied

: occupation! ( f u -- )
occupationmap + c! ;

: occupy< ( u -- u )
]] dup >r occupationmap + c@ 0= if true r@ occupation! r@ [[ ; immediate

: >occupy ( -- )
]] false r@ occupation! then rdrop [[ ; immediate

: try< ( run-time: -- u )
]] 10 0 do i occupy< [[ ; immediate

: >try ( run-time: -- )
]] >occupy loop [[ ; immediate

: .solution {: s e n d m o r y -- :}
s 0 .r e 0 .r n 0 .r d 0 .r ." +"
m 0 .r o 0 .r r 0 .r e 0 .r ." ="
m 0 .r o 0 .r n 0 .r e 0 .r y 0 .r ;

: smm ( -- )
\ SEND+MORE=MONEY
occupationmap 10 erase
1 occupy< {: m :}
try< {: d :}
try< {: e :}
d e + 10 /modf {: carry0 :} occupy< {: y :}
try< {: n :}
e carry0 - n - 10 /modf negate {: carry1 :} occupy< {: r :}
n carry1 - e - 10 /modf negate {: carry2 :} occupy< {: o :}
o carry2 - m - 10 /modf negate m = if occupy< {: s :}
cr s e n d m o r y .solution
>occupy else drop then
>occupy
>occupy
>try
>occupy
>try
>try
>occupy ;

This assumes that MONEY has no leading 0. Another option is to start
with M, derive S from that, which reduces the search space for the
rest; I did this for M, but not for S. I guess that with enough
smarts you need very few guesses, because this is a puzzle from the
times before computers.

Performance (with gforth-fast on a 4GHz Skylake):

minforth Ertl
6_428_853_392 39_964 cycles:u
19_625_679_081 114_372 instructions:u

Actually the Ertl solution was so fast that I ran it 1M times (and
divided the result by 1M) to make the Gforth startup overhead
insignificant.

One could probably make it even faster by keeping the occupation map
as a bitmap on the stack.

You can download the programs from

http://www.complang.tuwien.ac.at/forth/programs/sendmore.fth
http://www.complang.tuwien.ac.at/forth/programs/sendmore-ae.4th

- anton
--
M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
New standard: https://forth-standard.org/
EuroForth 2022: https://euro.theforth.net

Re: SEND+MORE=MONEY

<2023Feb8.114550@mips.complang.tuwien.ac.at>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=22283&group=comp.lang.forth#22283

  copy link   Newsgroups: comp.lang.forth
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: ant...@mips.complang.tuwien.ac.at (Anton Ertl)
Newsgroups: comp.lang.forth
Subject: Re: SEND+MORE=MONEY
Date: Wed, 08 Feb 2023 10:45:50 GMT
Organization: Institut fuer Computersprachen, Technische Universitaet Wien
Lines: 67
Message-ID: <2023Feb8.114550@mips.complang.tuwien.ac.at>
References: <78cfcd0a-5bc4-4584-8a92-9e8005a0e306n@googlegroups.com> <2023Feb8.093829@mips.complang.tuwien.ac.at>
Injection-Info: reader01.eternal-september.org; posting-host="51bbd3e42738c50cd53c2860b5745e5a";
logging-data="154876"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18wAgaIyyXKJyTFIAH0chvy"
Cancel-Lock: sha1:chjazs7AbLpu2Hd4m6iehXvMJx4=
X-newsreader: xrn 10.11
 by: Anton Ertl - Wed, 8 Feb 2023 10:45 UTC

anton@mips.complang.tuwien.ac.at (Anton Ertl) writes:
>Writing about it, I get inspired to try it myself:

And here's the code with execution conts in parentheses, produced with

gforth coverage.fs ~/forth/sendmore-ae.4th -e "smm cr bw-cover .coverage bye"

\ SEND+MORE=MONEY program by M. Anton Ertl 2023

create occupationmap 10 allot
\ each entry is 0 if free, non-0 if occupied

: occupation! ( 1252) ( f u -- )
( 1252) occupationmap + c! ;

: occupy< ( 8) ( u -- u )
( 8) ]] dup >r occupationmap + c@ 0= if true r@ occupation! r@ [[ ; immediate

: >occupy ( 8) ( -- )
( 8) ]] false r@ occupation! then rdrop [[ ; immediate

: try< ( 3) ( run-time: -- u )
( 3) ]] 10 0 do i occupy< [[ ; immediate

: >try ( 3) ( run-time: -- )
( 3) ]] >occupy loop [[ ; immediate

: .solution ( 1) {: s e n d m o r y -- :}
( 1) s 0 .r e 0 .r n 0 .r d 0 .r ." +"
( 1) m 0 .r o 0 .r r 0 .r e 0 .r ." ="
( 1) m 0 .r o 0 .r n 0 .r e 0 .r y 0 .r ;

: smm ( 1) ( -- )
( 1) \ SEND+MORE=MONEY
( 1) occupationmap 10 erase
( 1) 1 occupy< ( 1) {: m :}
( 1) try< ( 10) ( 9) {: d :}
( 9) try< ( 90) ( 72) {: e :}
( 72) d e + 10 /modf {: carry0 :} occupy< ( 48) {: y :}
( 48) try< ( 480) ( 288) {: n :}
( 288) e carry0 - n - 10 /modf negate {: carry1 :} occupy< ( 140) {: r :}
( 140) n carry1 - e - 10 /modf negate {: carry2 :} occupy< ( 67) {: o :}
( 67) o carry2 - m - 10 /modf negate m = if ( 15) occupy< ( 1) {: s :}
( 1) cr s e n d m o r y .solution
( 1) >occupy ( 15) else ( 52) drop then ( 67)
( 67) >occupy ( 140)
( 140) >occupy ( 288)
( 288) >try ( 480) ( 48)
( 48) >occupy ( 72)
( 72) >try ( 90) ( 9)
( 9) >try ( 10) ( 1)
( 1) >occupy ( 1) ;

Note that the two numbers after TRY< come from the DO and the IF
compiled by the TRY<: the first number is the number of loop
iterations, the second number time times the non-occupation test has
succeeded. E.g., the first TRY< is performed once "( 1)" at the start
of the line, it then performs 10 occupation checks, 9 of which
succeed.

- anton
--
M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
New standard: https://forth-standard.org/
EuroForth 2022: https://euro.theforth.net

Re: SEND+MORE=MONEY

<99bbe06a-adb4-42dd-870d-993fecc23ddbn@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=22284&group=comp.lang.forth#22284

  copy link   Newsgroups: comp.lang.forth
X-Received: by 2002:a37:6983:0:b0:71b:cdbd:5245 with SMTP id e125-20020a376983000000b0071bcdbd5245mr663569qkc.132.1675860664703;
Wed, 08 Feb 2023 04:51:04 -0800 (PST)
X-Received: by 2002:a25:9c05:0:b0:83f:9be5:45fa with SMTP id
c5-20020a259c05000000b0083f9be545famr828295ybo.457.1675860664519; Wed, 08 Feb
2023 04:51:04 -0800 (PST)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.forth
Date: Wed, 8 Feb 2023 04:51:04 -0800 (PST)
In-Reply-To: <bd72798b-c201-4267-a2ff-2621482d4650n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=79.224.102.188; posting-account=AqNUYgoAAADmkK2pN-RKms8sww57W0Iw
NNTP-Posting-Host: 79.224.102.188
References: <78cfcd0a-5bc4-4584-8a92-9e8005a0e306n@googlegroups.com> <bd72798b-c201-4267-a2ff-2621482d4650n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <99bbe06a-adb4-42dd-870d-993fecc23ddbn@googlegroups.com>
Subject: Re: SEND+MORE=MONEY
From: minfo...@arcor.de (minf...@arcor.de)
Injection-Date: Wed, 08 Feb 2023 12:51:04 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 2341
 by: minf...@arcor.de - Wed, 8 Feb 2023 12:51 UTC

Marcel Hendrix schrieb am Mittwoch, 8. Februar 2023 um 10:23:15 UTC+1:
> On Tuesday, February 7, 2023 at 7:34:08 PM UTC+1, minf...@arcor.de wrote:
> > Today I doodled with constraint logic programming in Forth.
> [..]
> > SENDMOREMONEY
>
> It is unclear what you want to do?
>

It is a very old mathematical puzzle, looking simple but surprisingly difficult
to solve by hand. Unique solution:
SEND + MORE = 9567 + 1085 = 10652 = MONEY

The manual way to go is to reduce the search space by applying algebraic
properties of addition operations like
M <> 0
S+M >= 9
etc

For computation the absolute benchmark would be CLP programming languages
like SICSTUS Prolog:
sum(S, E, N, D, M, O, R, Y) +:
1000*S + 100*E + 10*N + D
+ 1000*M + 100*O + 10*R + E
#= 10000*M + 1000*O + 100*N + 10*E + Y.

This is the complete program to solve the puzzle!
The magic happens within the #= operator.

Forth as an imperative language does not have an automatic backtracking solver
built in like Prolog, therefore in Forth one has to implement walking a search space
through manual looping or using generators.

Re: SEND+MORE=MONEY

<7cd623b2-0e4d-461f-9807-d17b0dbda103n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=22285&group=comp.lang.forth#22285

  copy link   Newsgroups: comp.lang.forth
X-Received: by 2002:ac8:7d94:0:b0:3b9:b43c:5db0 with SMTP id c20-20020ac87d94000000b003b9b43c5db0mr1271486qtd.132.1675862497366;
Wed, 08 Feb 2023 05:21:37 -0800 (PST)
X-Received: by 2002:a5b:b43:0:b0:86e:c769:e975 with SMTP id
b3-20020a5b0b43000000b0086ec769e975mr855647ybr.81.1675862497057; Wed, 08 Feb
2023 05:21:37 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!1.us.feeder.erje.net!3.us.feeder.erje.net!feeder.erje.net!border-1.nntp.ord.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.forth
Date: Wed, 8 Feb 2023 05:21:36 -0800 (PST)
In-Reply-To: <99bbe06a-adb4-42dd-870d-993fecc23ddbn@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=131.155.124.133; posting-account=-JQ2RQoAAAB6B5tcBTSdvOqrD1HpT_Rk
NNTP-Posting-Host: 131.155.124.133
References: <78cfcd0a-5bc4-4584-8a92-9e8005a0e306n@googlegroups.com>
<bd72798b-c201-4267-a2ff-2621482d4650n@googlegroups.com> <99bbe06a-adb4-42dd-870d-993fecc23ddbn@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <7cd623b2-0e4d-461f-9807-d17b0dbda103n@googlegroups.com>
Subject: Re: SEND+MORE=MONEY
From: mhx...@iae.nl (Marcel Hendrix)
Injection-Date: Wed, 08 Feb 2023 13:21:37 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 9
 by: Marcel Hendrix - Wed, 8 Feb 2023 13:21 UTC

On Wednesday, February 8, 2023 at 1:51:05 PM UTC+1, minf...@arcor.de wrote:
> Marcel Hendrix schrieb am Mittwoch, 8. Februar 2023 um 10:23:15 UTC+1:
> > On Tuesday, February 7, 2023 at 7:34:08 PM UTC+1, minf...@arcor.de wrote:
[..]
> SEND + MORE = 9567 + 1085 = 10652 = MONEY

Oh. All 3 numbers have a '5' and that is not a problem... I will have to look
elsewhere to get an exact description.

-marcel

Re: SEND+MORE=MONEY

<2023Feb8.183338@mips.complang.tuwien.ac.at>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=22287&group=comp.lang.forth#22287

  copy link   Newsgroups: comp.lang.forth
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: ant...@mips.complang.tuwien.ac.at (Anton Ertl)
Newsgroups: comp.lang.forth
Subject: Re: SEND+MORE=MONEY
Date: Wed, 08 Feb 2023 17:33:38 GMT
Organization: Institut fuer Computersprachen, Technische Universitaet Wien
Lines: 58
Message-ID: <2023Feb8.183338@mips.complang.tuwien.ac.at>
References: <78cfcd0a-5bc4-4584-8a92-9e8005a0e306n@googlegroups.com> <bd72798b-c201-4267-a2ff-2621482d4650n@googlegroups.com> <99bbe06a-adb4-42dd-870d-993fecc23ddbn@googlegroups.com>
Injection-Info: reader01.eternal-september.org; posting-host="51bbd3e42738c50cd53c2860b5745e5a";
logging-data="303125"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18xZ5ZmCWF1khO38odmyygq"
Cancel-Lock: sha1:yJL5JleqS+BaTLq39mRi6C8b854=
X-newsreader: xrn 10.11
 by: Anton Ertl - Wed, 8 Feb 2023 17:33 UTC

"minf...@arcor.de" <minforth@arcor.de> writes:
>For computation the absolute benchmark would be CLP programming languages
>like SICSTUS Prolog:
>sum(S, E, N, D, M, O, R, Y) +:
> 1000*S + 100*E + 10*N + D
> + 1000*M + 100*O + 10*R + E
> #= 10000*M + 1000*O + 100*N + 10*E + Y.
>
>This is the complete program to solve the puzzle!
>The magic happens within the #= operator.

Not even alldifferent([S,E,N,D,M,O,R,Y])? No way to influence the
labeling? Too much magic.

For comparsion, I did not do SEND+MORE=MONEY for my master's thesis
<https://www.complang.tuwien.ac.at/Diplomarbeiten/ertl90.ps.gz>, but I
did some others. E.g., the Forward-checking variant of the
N-Queens-Problem looked as follows:

queens(N,L):-
length(L,N),
L in 1..N,
alldifferent(L),
safe(L),
labeling(L).

length([],0).
length([X|Y],N):- N>0, plus(N1,1,N), length(Y,N1).

safe([]).
safe([X|Xs]):-
noattack(X,Xs),
safe(Xs).

noattack(X,L):- noattack(X,L,1).

noattack(X,[],Nb).
noattack(X,[Y|Ys],Nb):-
notsum(X,Nb,Y),
notsum(Y,Nb,X),
Nb1 is Nb+1,
noattack(X,Ys,Nb1).

notsum(X,Y,Z):- notplus(X,Y,Z).

labeling([]).
labeling([X|Y]):- indomain(X), labeling(Y).

And this allowed to, e.g., refine the labeling for a more efficient
order of variables and values used in labeling (shown in Abbildung 6.6
and 6.7).

- anton
--
M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
New standard: https://forth-standard.org/
EuroForth 2022: https://euro.theforth.net

Re: SEND+MORE=MONEY

<c197f7fa-5c19-4475-85e2-9e2468d00ddfn@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=22289&group=comp.lang.forth#22289

  copy link   Newsgroups: comp.lang.forth
X-Received: by 2002:a37:2d84:0:b0:70d:cda:a6e7 with SMTP id t126-20020a372d84000000b0070d0cdaa6e7mr717216qkh.57.1675879849871;
Wed, 08 Feb 2023 10:10:49 -0800 (PST)
X-Received: by 2002:a0d:d844:0:b0:50a:468d:48d8 with SMTP id
a65-20020a0dd844000000b0050a468d48d8mr1146693ywe.104.1675879849690; Wed, 08
Feb 2023 10:10:49 -0800 (PST)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.forth
Date: Wed, 8 Feb 2023 10:10:49 -0800 (PST)
In-Reply-To: <2023Feb8.183338@mips.complang.tuwien.ac.at>
Injection-Info: google-groups.googlegroups.com; posting-host=2003:f7:1f2e:e920:2570:3f7f:1cee:b256;
posting-account=AqNUYgoAAADmkK2pN-RKms8sww57W0Iw
NNTP-Posting-Host: 2003:f7:1f2e:e920:2570:3f7f:1cee:b256
References: <78cfcd0a-5bc4-4584-8a92-9e8005a0e306n@googlegroups.com>
<bd72798b-c201-4267-a2ff-2621482d4650n@googlegroups.com> <99bbe06a-adb4-42dd-870d-993fecc23ddbn@googlegroups.com>
<2023Feb8.183338@mips.complang.tuwien.ac.at>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <c197f7fa-5c19-4475-85e2-9e2468d00ddfn@googlegroups.com>
Subject: Re: SEND+MORE=MONEY
From: minfo...@arcor.de (minf...@arcor.de)
Injection-Date: Wed, 08 Feb 2023 18:10:49 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 2485
 by: minf...@arcor.de - Wed, 8 Feb 2023 18:10 UTC

Anton Ertl schrieb am Mittwoch, 8. Februar 2023 um 18:48:49 UTC+1:
> "minf...@arcor.de" <minf...@arcor.de> writes:
> >For computation the absolute benchmark would be CLP programming languages
> >like SICSTUS Prolog:
> >sum(S, E, N, D, M, O, R, Y) +:
> > 1000*S + 100*E + 10*N + D
> > + 1000*M + 100*O + 10*R + E
> > #= 10000*M + 1000*O + 100*N + 10*E + Y.
> >
> >This is the complete program to solve the puzzle!
> >The magic happens within the #= operator.
> Not even alldifferent([S,E,N,D,M,O,R,Y])? No way to influence the
> labeling? Too much magic.

This justified remark comes from my too abbreviated example. If you want to see
the car and not just the motor, here it is:

:- use_module(library(clpfd)).

mm([S,E,N,D,M,O,R,Y], Type) :-
domain([S,E,N,D,M,O,R,Y], 0, 9), % step 1
S#>0, M#>0,
all_different([S,E,N,D,M,O,R,Y]), % step 2
sum(S,E,N,D,M,O,R,Y),
labeling(Type, [S,E,N,D,M,O,R,Y]). % step 3

sum(S, E, N, D, M, O, R, Y) :-
1000*S + 100*E + 10*N + D
+ 1000*M + 100*O + 10*R + E
#= 10000*M + 1000*O + 100*N + 10*E + Y.

| ?- mm([S,E,N,D,M,O,R,Y], []).
D = 7,
E = 5,
M = 1,
N = 6,
O = 0,
R = 8,
S = 9,
Y = 2

Re: SEND+MORE=MONEY

<06aa4095-54e6-4745-afdf-e01616ee7944n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=22290&group=comp.lang.forth#22290

  copy link   Newsgroups: comp.lang.forth
X-Received: by 2002:a05:622a:1309:b0:3b8:6cc5:9d7 with SMTP id v9-20020a05622a130900b003b86cc509d7mr1327730qtk.154.1675881071105;
Wed, 08 Feb 2023 10:31:11 -0800 (PST)
X-Received: by 2002:a25:f504:0:b0:8c7:f1f7:35a4 with SMTP id
a4-20020a25f504000000b008c7f1f735a4mr213029ybe.104.1675881070829; Wed, 08 Feb
2023 10:31:10 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.forth
Date: Wed, 8 Feb 2023 10:31:10 -0800 (PST)
In-Reply-To: <c197f7fa-5c19-4475-85e2-9e2468d00ddfn@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=2003:f7:1f2e:e920:2570:3f7f:1cee:b256;
posting-account=AqNUYgoAAADmkK2pN-RKms8sww57W0Iw
NNTP-Posting-Host: 2003:f7:1f2e:e920:2570:3f7f:1cee:b256
References: <78cfcd0a-5bc4-4584-8a92-9e8005a0e306n@googlegroups.com>
<bd72798b-c201-4267-a2ff-2621482d4650n@googlegroups.com> <99bbe06a-adb4-42dd-870d-993fecc23ddbn@googlegroups.com>
<2023Feb8.183338@mips.complang.tuwien.ac.at> <c197f7fa-5c19-4475-85e2-9e2468d00ddfn@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <06aa4095-54e6-4745-afdf-e01616ee7944n@googlegroups.com>
Subject: Re: SEND+MORE=MONEY
From: minfo...@arcor.de (minf...@arcor.de)
Injection-Date: Wed, 08 Feb 2023 18:31:11 +0000
Content-Type: text/plain; charset="UTF-8"
 by: minf...@arcor.de - Wed, 8 Feb 2023 18:31 UTC

minf...@arcor.de schrieb am Mittwoch, 8. Februar 2023 um 19:10:51 UTC+1:
> Anton Ertl schrieb am Mittwoch, 8. Februar 2023 um 18:48:49 UTC+1:
> > "minf...@arcor.de" <minf...@arcor.de> writes:
> > >For computation the absolute benchmark would be CLP programming languages
> > >like SICSTUS Prolog:
> > >sum(S, E, N, D, M, O, R, Y) +:
> > > 1000*S + 100*E + 10*N + D
> > > + 1000*M + 100*O + 10*R + E
> > > #= 10000*M + 1000*O + 100*N + 10*E + Y.
> > >
> > >This is the complete program to solve the puzzle!
> > >The magic happens within the #= operator.
> > Not even alldifferent([S,E,N,D,M,O,R,Y])? No way to influence the
> > labeling? Too much magic.
> This justified remark comes from my too abbreviated example. If you want to see
> the car and not just the motor, here it is:
>
> :- use_module(library(clpfd)).
>
> mm([S,E,N,D,M,O,R,Y], Type) :-
> domain([S,E,N,D,M,O,R,Y], 0, 9), % step 1
> S#>0, M#>0,
> all_different([S,E,N,D,M,O,R,Y]), % step 2
> sum(S,E,N,D,M,O,R,Y),
> labeling(Type, [S,E,N,D,M,O,R,Y]). % step 3
>
> sum(S, E, N, D, M, O, R, Y) :-
> 1000*S + 100*E + 10*N + D
> + 1000*M + 100*O + 10*R + E
> #= 10000*M + 1000*O + 100*N + 10*E + Y.
> | ?- mm([S,E,N,D,M,O,R,Y], []).
> D = 7,
> E = 5,
> M = 1,
> N = 6,
> O = 0,
> R = 8,
> S = 9,
> Y = 2

P. S. FWIW my old playhorse had been BProlog, not as versatile as alpha male
Sicstus, but still a joy to use. BProlog example (now the car) in some few lines:

sendmore(Digits) :-
Digits = [S,E,N,D,M,O,R,Y], % Create variables
Digits :: [0..9], % Associate domains to variables
S #\= 0, % Add. constraints
M #\= 0,
alldifferent(Digits), % all digits must be uniqe
1000*S + 100*E + 10*N + D % Main constraints
+ 1000*M + 100*O + 10*R + E
#= 10000*M + 1000*O + 100*N + 10*E + Y,
labeling(Digits). % Solve it

P. P. S. I like your Queens problem solver! I'll have to study it deeper. Unfortunately
right now the weather here is just too fine for indoor studies. ;-)

Re: SEND+MORE=MONEY

<87ttzvzmmb.fsf@nightsong.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=22294&group=comp.lang.forth#22294

  copy link   Newsgroups: comp.lang.forth
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: no.em...@nospam.invalid (Paul Rubin)
Newsgroups: comp.lang.forth
Subject: Re: SEND+MORE=MONEY
Date: Wed, 08 Feb 2023 16:52:12 -0800
Organization: A noiseless patient Spider
Lines: 32
Message-ID: <87ttzvzmmb.fsf@nightsong.com>
References: <78cfcd0a-5bc4-4584-8a92-9e8005a0e306n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: reader01.eternal-september.org; posting-host="e5409055a028117a873680ba62c07629";
logging-data="440987"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+IeIZZp0QOHFWrBuAMMdCE"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:U8tO9VAtkgy2ysfua0xSFu0mKi0=
sha1:SscTvHEs0pjcOVMCqvXQ29Ycfu0=
 by: Paul Rubin - Thu, 9 Feb 2023 00:52 UTC

"minf...@arcor.de" <minforth@arcor.de> writes:
> A classic beginner's example is the SEND+MORE=MONEY puzzlee,

I spent about an hour messing with the below algorithm in Forth, but got
somewhat bogged down generating the permutations recursively because I
had a DO loop that might have been putting loop indices on the return
stack and interfering with the recursion. I put it aside and did it in
Python (below). I may get back to the Forth version, but meanwhile,
this was much easier:

from itertools import permutations
from functools import reduce

def main():
def digits(*ds): return reduce(lambda a,b: 10*a+b, ds, 0)

for x1,x2,s,e,n,d,m,o,r,y in permutations(range(10)):
if x1 <= x2 or m == 0: continue
send = digits(s,e,n,d)
more = digits(m,o,r,e)
money = digits(m,o,n,e,y)
if send+more==money: print(send,more,money)
main()

Output:

$ time python3 smm.py
9567 1085 10652

real 0m3.966s
user 0m3.960s
sys 0m0.001s

Re: SEND+MORE=MONEY

<787c9189-9528-4a55-8462-8e924c68ff7cn@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=22297&group=comp.lang.forth#22297

  copy link   Newsgroups: comp.lang.forth
X-Received: by 2002:a0c:8c04:0:b0:556:57ff:64d1 with SMTP id n4-20020a0c8c04000000b0055657ff64d1mr914299qvb.8.1675929564240;
Wed, 08 Feb 2023 23:59:24 -0800 (PST)
X-Received: by 2002:a0d:c546:0:b0:506:6dab:e1b3 with SMTP id
h67-20020a0dc546000000b005066dabe1b3mr1014002ywd.467.1675929564015; Wed, 08
Feb 2023 23:59:24 -0800 (PST)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.forth
Date: Wed, 8 Feb 2023 23:59:23 -0800 (PST)
In-Reply-To: <78cfcd0a-5bc4-4584-8a92-9e8005a0e306n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=154.121.48.163; posting-account=KJSw4AoAAACRkUCek5r_78mFj6sHzH4C
NNTP-Posting-Host: 154.121.48.163
References: <78cfcd0a-5bc4-4584-8a92-9e8005a0e306n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <787c9189-9528-4a55-8462-8e924c68ff7cn@googlegroups.com>
Subject: Re: SEND+MORE=MONEY
From: ahmed.me...@univ-bejaia.dz (Ahmed MELAHI)
Injection-Date: Thu, 09 Feb 2023 07:59:24 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 7260
 by: Ahmed MELAHI - Thu, 9 Feb 2023 07:59 UTC

Le mardi 7 février 2023 à 18:34:08 UTC, minf...@arcor.de a écrit :
> Today I doodled with constraint logic programming in Forth.
>
> A classic beginner's example is the SEND+MORE=MONEY puzzlee,
> where each letter stands for a digit in the range of 0 to 9
> and which when concatenated represent a decimal number.
> Constraint: all digits must be different.
>
> I came up with the little program below using brute force.
> It does its job, but ugly. Any ideas for improvement and acceleration?
> Or syntax-wise? (Prolog does it so much more nicely).
>
> \ ##### SENDMORE.FTH #####
> : ALLDIFFERENT {: a b c d e f g h -- flag :}
> false
> a b = IF exit THEN
> a c = IF exit THEN
> a d = IF exit THEN
> a e = IF exit THEN
> a f = IF exit THEN
> a g = IF exit THEN
> a h = IF exit THEN
> b c = IF exit THEN
> b d = IF exit THEN
> b e = IF exit THEN
> b f = IF exit THEN
> b g = IF exit THEN
> b h = IF exit THEN
> c d = IF exit THEN
> c e = IF exit THEN
> c f = IF exit THEN
> c g = IF exit THEN
> c h = IF exit THEN
> d e = IF exit THEN
> d f = IF exit THEN
> d g = IF exit THEN
> d h = IF exit THEN
> e f = IF exit THEN
> e g = IF exit THEN
> e h = IF exit THEN
> f g = IF exit THEN
> f h = IF exit THEN
> g h = IF exit THEN
> drop true ;
>
> : SENDMOREMONEY {: | s e n d m o r y s1 s2 s3 ct -- :}
> 0 to ct
> 1 9 DO i to s
> 1 9 DO i to m
> 1 9 DO i to e
> 1 9 DO i to d
> 0 9 DO i to n
> 0 9 DO i to o
> 0 9 DO i to r
> 0 9 DO i to y
> s e n d m o r y alldifferent
> IF
> ct 1+ to ct
> s 1000 * e 100 * + n 10 * + d + to s1
> m 1000 * o 100 * + r 10 * + e + to s2
> m 10000 * o 1000 * + n 100 * + e 10 * + y + to s3
> s1 s2 + s3 =
> IF
> cr ." S=" s . ." E=" e . ." N=" n . ." D=" d .
> cr ." M=" m . ." O=" o . ." R=" r . ." Y=" y .
> cr ." " s1 . cr ." +" s2 . cr ." -----" cr ." " s3 .
> THEN
> THEN
> -1 +LOOP -1 +LOOP -1 +LOOP -1 +LOOP -1 +LOOP -1 +LOOP -1 +LOOP -1 +LOOP
> ." loops:" ct . ;
>
> SENDMOREMONEY
Hi everybody,
Here is a program that gives all possible solutions (there are 25), written in gforth.

s" random.fs" included

: not 0= ;

0 value s
0 value e
0 value n
0 value d
0 value m
0 value o
0 value r
0 value y

: send s 10 * e + 10 * n + 10 * d + ;
: more m 10 * o + 10 * r + 10 * e + ;
: money m 10 * o + 10 * n + 10 * e + 10 * y + ;

: is_send+more=money_? send more + money - 0= ;

create flags_chosen_vals 10 allot
flags_chosen_vals 10 erase
create flags_chosen_sendmory 8 allot
flags_chosen_sendmory 8 erase

: choose 10 random ;
: chosen flags_chosen_vals + 1 swap c! ;
: chosen_? flags_chosen_vals + c@ ;

: chosen_var_set flags_chosen_sendmory + 1 swap c! ;
: chosen_var_get flags_chosen_sendmory + c@ ;

: chosen--> dup chosen ;

: chosen_vars_init flags_chosen_sendmory 8 erase ;
: chosen_vals_init flags_chosen_vals 10 erase ;

: to_s choose chosen--> to s 0 chosen_var_set ;
: to_e choose dup chosen_? if drop else chosen--> to e 1 chosen_var_set then ;
: to_n choose dup chosen_? if drop else chosen--> to n 2 chosen_var_set then ;
: to_d choose dup chosen_? if drop else chosen--> to d 3 chosen_var_set then ;
: to_m choose dup chosen_? if drop else chosen--> to m 4 chosen_var_set then ;
: to_o choose dup chosen_? if drop else chosen--> to o 5 chosen_var_set then ;
: to_r choose dup chosen_? if drop else chosen--> to r 6 chosen_var_set then ;
: to_y choose dup chosen_? if drop else chosen--> to y 7 chosen_var_set then ;
: gen_sendmory
chosen_vals_init
chosen_vars_init
to_s
begin 1 chosen_var_get not while to_e repeat
begin 2 chosen_var_get not while to_n repeat
begin 3 chosen_var_get not while to_d repeat
begin 4 chosen_var_get not while to_m repeat
begin 5 chosen_var_get not while to_o repeat
begin 6 chosen_var_get not while to_r repeat
begin 7 chosen_var_get not while to_y repeat
;

: .sendmory s . e . n . d . m . o . r . y . ;

: sendmory_as_number s 10 * e + 10 * n + 10 * d + 10 * m + 10 * o + 10 * r + 10 * y + ;

1000 value max_results_size
create results max_results_size cells allot
0 value result_counter

: to_results
sendmory_as_number
result_counter 0 ?do
dup i cells results + @ = if
drop unloop exit
then
loop
result_counter cells results + !
result_counter 1+ to result_counter
result_counter cr . ." solutions found"
;

: go_sendmoremoney
0 to result_counter
0 do
gen_sendmory
is_send+more=money_? if
( cr .sendmory)
to_results
( unloop exit)
then
loop
cr cr result_counter . ." solutions found"
;

: .send send 4 .r ;
: .more more 4 .r ;
: .money money 5 .r ;

: to_sendmory
10000000 /mod to s
1000000 /mod to e
100000 /mod to n
10000 /mod to d
1000 /mod to m
100 /mod to o
10 /mod to r
to y
;


: .send+more=money .send ." + " .more ." = " .money ;

: .results
." sol_num sendmory send + more = money"
result_counter 0 ?do
cr i 7 .r 3 spaces
i cells results + @ dup
. 3 spaces
to_sendmory .send+more=money
loop
;
: go go_sendmoremoney cr .results ;

10000000 go

Bye

Re: SEND+MORE=MONEY

<7f3b51ea-1680-4d80-890b-28ac718d288fn@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=22298&group=comp.lang.forth#22298

  copy link   Newsgroups: comp.lang.forth
X-Received: by 2002:a0c:e307:0:b0:537:635a:a3d5 with SMTP id s7-20020a0ce307000000b00537635aa3d5mr953964qvl.48.1675930396901;
Thu, 09 Feb 2023 00:13:16 -0800 (PST)
X-Received: by 2002:a81:7387:0:b0:527:b74c:bbee with SMTP id
o129-20020a817387000000b00527b74cbbeemr1150570ywc.322.1675930396614; Thu, 09
Feb 2023 00:13:16 -0800 (PST)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.forth
Date: Thu, 9 Feb 2023 00:13:16 -0800 (PST)
In-Reply-To: <787c9189-9528-4a55-8462-8e924c68ff7cn@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=2003:f7:1f2e:e920:2570:3f7f:1cee:b256;
posting-account=AqNUYgoAAADmkK2pN-RKms8sww57W0Iw
NNTP-Posting-Host: 2003:f7:1f2e:e920:2570:3f7f:1cee:b256
References: <78cfcd0a-5bc4-4584-8a92-9e8005a0e306n@googlegroups.com> <787c9189-9528-4a55-8462-8e924c68ff7cn@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <7f3b51ea-1680-4d80-890b-28ac718d288fn@googlegroups.com>
Subject: Re: SEND+MORE=MONEY
From: minfo...@arcor.de (minf...@arcor.de)
Injection-Date: Thu, 09 Feb 2023 08:13:16 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 1645
 by: minf...@arcor.de - Thu, 9 Feb 2023 08:13 UTC

Ahmed MELAHI schrieb am Donnerstag, 9. Februar 2023 um 09:00:14 UTC+1:
> > SENDMOREMONEY
> Hi everybody,
> Here is a program that gives all possible solutions (there are 25), written in gforth.

Thanks! 25 solutions appear when M is allowed to be zero.
Even more solutions appear when the all-digits-different constraint is taken out.

I am fascinated by those many completely different approaches in this thread.

Re: SEND+MORE=MONEY

<feb3b1c8-c680-480e-8a9f-c29e202a1ab7n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=22299&group=comp.lang.forth#22299

  copy link   Newsgroups: comp.lang.forth
X-Received: by 2002:a05:622a:181:b0:3b9:e0b5:1f8e with SMTP id s1-20020a05622a018100b003b9e0b51f8emr1446713qtw.399.1675930812885;
Thu, 09 Feb 2023 00:20:12 -0800 (PST)
X-Received: by 2002:a5b:28b:0:b0:884:b6d1:6dd1 with SMTP id
x11-20020a5b028b000000b00884b6d16dd1mr934888ybl.237.1675930812655; Thu, 09
Feb 2023 00:20:12 -0800 (PST)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.forth
Date: Thu, 9 Feb 2023 00:20:12 -0800 (PST)
In-Reply-To: <7cd623b2-0e4d-461f-9807-d17b0dbda103n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=2003:f7:1f2e:e920:2570:3f7f:1cee:b256;
posting-account=AqNUYgoAAADmkK2pN-RKms8sww57W0Iw
NNTP-Posting-Host: 2003:f7:1f2e:e920:2570:3f7f:1cee:b256
References: <78cfcd0a-5bc4-4584-8a92-9e8005a0e306n@googlegroups.com>
<bd72798b-c201-4267-a2ff-2621482d4650n@googlegroups.com> <99bbe06a-adb4-42dd-870d-993fecc23ddbn@googlegroups.com>
<7cd623b2-0e4d-461f-9807-d17b0dbda103n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <feb3b1c8-c680-480e-8a9f-c29e202a1ab7n@googlegroups.com>
Subject: Re: SEND+MORE=MONEY
From: minfo...@arcor.de (minf...@arcor.de)
Injection-Date: Thu, 09 Feb 2023 08:20:12 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 1895
 by: minf...@arcor.de - Thu, 9 Feb 2023 08:20 UTC

Marcel Hendrix schrieb am Mittwoch, 8. Februar 2023 um 14:21:38 UTC+1:
> On Wednesday, February 8, 2023 at 1:51:05 PM UTC+1, minf...@arcor.de wrote:
> > Marcel Hendrix schrieb am Mittwoch, 8. Februar 2023 um 10:23:15 UTC+1:
> > > On Tuesday, February 7, 2023 at 7:34:08 PM UTC+1, minf...@arcor.de wrote:
> [..]
> > SEND + MORE = 9567 + 1085 = 10652 = MONEY
> Oh. All 3 numbers have a '5' and that is not a problem... I will have to look
> elsewhere to get an exact description.
>
Here you go:
https://en.wikipedia.org/wiki/Verbal_arithmetic

Re: SEND+MORE=MONEY

<2023Feb9.084758@mips.complang.tuwien.ac.at>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=22300&group=comp.lang.forth#22300

  copy link   Newsgroups: comp.lang.forth
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: ant...@mips.complang.tuwien.ac.at (Anton Ertl)
Newsgroups: comp.lang.forth
Subject: Re: SEND+MORE=MONEY
Date: Thu, 09 Feb 2023 07:47:58 GMT
Organization: Institut fuer Computersprachen, Technische Universitaet Wien
Lines: 47
Message-ID: <2023Feb9.084758@mips.complang.tuwien.ac.at>
References: <78cfcd0a-5bc4-4584-8a92-9e8005a0e306n@googlegroups.com> <87ttzvzmmb.fsf@nightsong.com>
Injection-Info: reader01.eternal-september.org; posting-host="4a23064dba6cc3b448d0d7b13f318f12";
logging-data="669839"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/wRACkmumBBOHoK4BuJ34l"
Cancel-Lock: sha1:uCJB9lQ2J8Xq0Xzgsi83HuYZjiA=
X-newsreader: xrn 10.11
 by: Anton Ertl - Thu, 9 Feb 2023 07:47 UTC

Paul Rubin <no.email@nospam.invalid> writes:
>$ time python3 smm.py
>9567 1085 10652
>
>real 0m3.966s
>user 0m3.960s
>sys 0m0.001s

I tried it on my 4GHz Skylake, where it is a little faster (2.54s user
time), but still slower than minforth's version. Cycles and
instructions:

Rubin minforth Ertl
9_969_117_645 6_428_853_392 39_964 cycles:u
31_937_879_569 19_625_679_081 114_372 instructions:u

You only generate 10!=3_628_800 permutations, while minforth generates
100_000_000 variants that he checks for the alldifferent property only
afterwards, but apparently the constant factor of Python3 is so much
worse than that of gforth-fast that minforth's version prevails.

I have now also tried minforth's version on several Forth systems for
performance comparison:

gforth-fast lxf SwiftForth 3.11 VFX 4.72
6_428_853_392 2_025_393_969 9_556_676_271 5_941_247_227 cycles:u
19_625_679_081 5_033_144_283 13_662_547_049 9_003_308_671 instructions:u

Let's see if SwiftForth and VFX are better in newer versions (on a Zen3):

gforth-fast lxf sf 4.0.0-RC52 VFX 64 5.11
5_996_105_179 1_661_344_432 5_205_443_081 6_991_357_051 cycles:u
19_625_279_724 5_033_144_483 11_408_344_112 9_084_604_055 instructions:u

My guess is that the locals in ALLDIFFERENT play a large role in the
performance. Even with the mediocre locals implementation of
SwiftForth and VFX, it's surprising that gforth-fast is so close to
VFX and SwiftForth, even beating the old SwiftForth and the new VFX;
after all, Gforth's locals implementation is not that great, either.
lxf demonstrates that locals can be implemented much faster.

- anton
--
M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
New standard: https://forth-standard.org/
EuroForth 2022: https://euro.theforth.net

Re: SEND+MORE=MONEY

<a61c497f-56a2-42e8-97fa-c87d7a82115bn@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=22301&group=comp.lang.forth#22301

  copy link   Newsgroups: comp.lang.forth
X-Received: by 2002:ac8:5dc6:0:b0:3b6:2e5d:9b63 with SMTP id e6-20020ac85dc6000000b003b62e5d9b63mr1109642qtx.192.1675932268385;
Thu, 09 Feb 2023 00:44:28 -0800 (PST)
X-Received: by 2002:a5b:341:0:b0:8cd:bb20:110a with SMTP id
q1-20020a5b0341000000b008cdbb20110amr333688ybp.457.1675932268054; Thu, 09 Feb
2023 00:44:28 -0800 (PST)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.forth
Date: Thu, 9 Feb 2023 00:44:27 -0800 (PST)
In-Reply-To: <2023Feb9.084758@mips.complang.tuwien.ac.at>
Injection-Info: google-groups.googlegroups.com; posting-host=85.76.41.253; posting-account=kiOBZQoAAADFsAs31ZHaefxTuQxv84Wm
NNTP-Posting-Host: 85.76.41.253
References: <78cfcd0a-5bc4-4584-8a92-9e8005a0e306n@googlegroups.com>
<87ttzvzmmb.fsf@nightsong.com> <2023Feb9.084758@mips.complang.tuwien.ac.at>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <a61c497f-56a2-42e8-97fa-c87d7a82115bn@googlegroups.com>
Subject: Re: SEND+MORE=MONEY
From: jali.hei...@gmail.com (Jali Heinonen)
Injection-Date: Thu, 09 Feb 2023 08:44:28 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 3676
 by: Jali Heinonen - Thu, 9 Feb 2023 08:44 UTC

torstai 9. helmikuuta 2023 klo 10.32.34 UTC+2 Anton Ertl kirjoitti:
> Paul Rubin <no.e...@nospam.invalid> writes:
> >$ time python3 smm.py
> >9567 1085 10652
> >
> >real 0m3.966s
> >user 0m3.960s
> >sys 0m0.001s
> I tried it on my 4GHz Skylake, where it is a little faster (2.54s user
> time), but still slower than minforth's version. Cycles and
> instructions:
>
> Rubin minforth Ertl
> 9_969_117_645 6_428_853_392 39_964 cycles:u
> 31_937_879_569 19_625_679_081 114_372 instructions:u
>
> You only generate 10!=3_628_800 permutations, while minforth generates
> 100_000_000 variants that he checks for the alldifferent property only
> afterwards, but apparently the constant factor of Python3 is so much
> worse than that of gforth-fast that minforth's version prevails.
>
> I have now also tried minforth's version on several Forth systems for
> performance comparison:
>
> gforth-fast lxf SwiftForth 3.11 VFX 4.72
> 6_428_853_392 2_025_393_969 9_556_676_271 5_941_247_227 cycles:u
> 19_625_679_081 5_033_144_283 13_662_547_049 9_003_308_671 instructions:u
>
> Let's see if SwiftForth and VFX are better in newer versions (on a Zen3):
>
> gforth-fast lxf sf 4.0.0-RC52 VFX 64 5.11
> 5_996_105_179 1_661_344_432 5_205_443_081 6_991_357_051 cycles:u
> 19_625_279_724 5_033_144_483 11_408_344_112 9_084_604_055 instructions:u
>
> My guess is that the locals in ALLDIFFERENT play a large role in the
> performance. Even with the mediocre locals implementation of
> SwiftForth and VFX, it's surprising that gforth-fast is so close to
> VFX and SwiftForth, even beating the old SwiftForth and the new VFX;
> after all, Gforth's locals implementation is not that great, either.
> lxf demonstrates that locals can be implemented much faster.
> - anton
> --
> M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.html
> comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
> New standard: https://forth-standard.org/
> EuroForth 2022: https://euro.theforth.net

Can ALLDIFFERENT be eliminated by using bit presentation for numbers, where bit position directly maps to number? Now, jus bitwise OR all the numbers, bitwise NOT and use bit twiddling trick to get the trailing zero bits to get the first possible different number candidate?

Re: SEND+MORE=MONEY

<138b28de-6f2a-449b-9279-629f3c17a03bn@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=22303&group=comp.lang.forth#22303

  copy link   Newsgroups: comp.lang.forth
X-Received: by 2002:a37:43d3:0:b0:71c:3b4e:9a47 with SMTP id q202-20020a3743d3000000b0071c3b4e9a47mr1063025qka.416.1675935225126;
Thu, 09 Feb 2023 01:33:45 -0800 (PST)
X-Received: by 2002:a25:ec0a:0:b0:875:1c0e:981a with SMTP id
j10-20020a25ec0a000000b008751c0e981amr1296712ybh.530.1675935224968; Thu, 09
Feb 2023 01:33:44 -0800 (PST)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.forth
Date: Thu, 9 Feb 2023 01:33:44 -0800 (PST)
In-Reply-To: <2023Feb9.084758@mips.complang.tuwien.ac.at>
Injection-Info: google-groups.googlegroups.com; posting-host=2003:f7:1f2e:e920:2570:3f7f:1cee:b256;
posting-account=AqNUYgoAAADmkK2pN-RKms8sww57W0Iw
NNTP-Posting-Host: 2003:f7:1f2e:e920:2570:3f7f:1cee:b256
References: <78cfcd0a-5bc4-4584-8a92-9e8005a0e306n@googlegroups.com>
<87ttzvzmmb.fsf@nightsong.com> <2023Feb9.084758@mips.complang.tuwien.ac.at>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <138b28de-6f2a-449b-9279-629f3c17a03bn@googlegroups.com>
Subject: Re: SEND+MORE=MONEY
From: minfo...@arcor.de (minf...@arcor.de)
Injection-Date: Thu, 09 Feb 2023 09:33:45 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 1633
 by: minf...@arcor.de - Thu, 9 Feb 2023 09:33 UTC

Anton Ertl schrieb am Donnerstag, 9. Februar 2023 um 09:32:34 UTC+1:
> My guess is that the locals in ALLDIFFERENT play a large role in the
> performance.

Put the 8 digits in global values and at least they have not to be copied so
many times around. IMO here we see a price ( implementation differences
put aside ) to be paid for Forth locals that have to be moved away from
the data stack.

Re: SEND+MORE=MONEY

<2023Feb9.100025@mips.complang.tuwien.ac.at>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=22304&group=comp.lang.forth#22304

  copy link   Newsgroups: comp.lang.forth
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: ant...@mips.complang.tuwien.ac.at (Anton Ertl)
Newsgroups: comp.lang.forth
Subject: Re: SEND+MORE=MONEY
Date: Thu, 09 Feb 2023 09:00:25 GMT
Organization: Institut fuer Computersprachen, Technische Universitaet Wien
Lines: 84
Message-ID: <2023Feb9.100025@mips.complang.tuwien.ac.at>
References: <78cfcd0a-5bc4-4584-8a92-9e8005a0e306n@googlegroups.com> <87ttzvzmmb.fsf@nightsong.com> <2023Feb9.084758@mips.complang.tuwien.ac.at> <a61c497f-56a2-42e8-97fa-c87d7a82115bn@googlegroups.com>
Injection-Info: reader01.eternal-september.org; posting-host="4a23064dba6cc3b448d0d7b13f318f12";
logging-data="700506"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18CvcvPJnlr8lepCadUy2Zb"
Cancel-Lock: sha1:C+478YcTANi7QJG6QHtYi6N42Wg=
X-newsreader: xrn 10.11
 by: Anton Ertl - Thu, 9 Feb 2023 09:00 UTC

Jali Heinonen <jali.heinonen@gmail.com> writes:
>Can ALLDIFFERENT be eliminated by using bit presentation for numbers, where=
> bit position directly maps to number? Now, jus bitwise OR all the numbers,=
> bitwise NOT and use bit twiddling trick to get the trailing zero bits to g=
>et the first possible different number candidate?

Certainly. Or you can use the in-memory occupation map that I used.
I just took that part from my program and adapted minforth's program
to use it, resulting in

http://www.complang.tuwien.ac.at/forth/programs/sendmore-hybrid.4th

Performance on Zen3 (with gforth-fast):

minforth hybrid
6_065_825_773 306_696_520 cycles:u
19_625_595_531 689_828_810 instructions:u

This approach certainly reduces the execution time; by far not as much
as testing each digit as soon as possible (or directly generating it),
like sendmore-ae.4th, but then sendmore-ae.4th needed more effort when
writing, and I had to fix two bugs, whereas sendmore-hybrid.4th worked
first time I tried it.

And here's the program with execution counts:

\ SEND+MORE=MONEY program by M. Anton Ertl 2023

create occupationmap 10 allot
\ each entry is 0 if free, non-0 if occupied

: occupation! ( 4170422) ( f u -- )
( 4170422) occupationmap + c! ;

: occupy< ( 8) ( u -- u )
( 8) ]] dup >r occupationmap + c@ 0= if true r@ occupation! r@ [[ ; immediate

: >occupy ( 8) ( -- )
( 8) ]] false r@ occupation! then rdrop [[ ; immediate

: try< ( 8) ( run-time: -- u )
( 8) ]] 10 0 do i occupy< [[ ; immediate

: >try ( 8) ( run-time: -- )
( 8) ]] >occupy loop [[ ; immediate

: .solution ( 1) {: s e n d m o r y -- :}
( 1) s 0 .r e 0 .r n 0 .r d 0 .r ." +"
( 1) m 0 .r o 0 .r r 0 .r e 0 .r ." ="
( 1) m 0 .r o 0 .r n 0 .r e 0 .r y 0 .r ;

: smm ( 1) ( -- )
( 1) \ SEND+MORE=MONEY
( 1) occupationmap 10 erase
( 1) try< ( 10) ( 10) {: s :} s 0 > if ( 9)
( 9) try< ( 90) ( 81) {: m :} m 0 > if ( 72)
( 72) try< ( 720) ( 576) {: e :}
( 576) try< ( 5760) ( 4032) {: d :}
( 4032) try< ( 40320) ( 24192) {: n :}
( 24192) try< ( 241920) ( 120960) {: o :}
( 120960) try< ( 1209600) ( 483840) {: r :}
( 483840) try< ( 4838400) ( 1451520) {: y :}
( 1451520) s 1000 * e 100 * + n 10 * + d + {: send :}
( 1451520) m 1000 * o 100 * + r 10 * + e + {: more :}
( 1451520) m 10000 * o 1000 * + n 100 * + e 10 * + y + {: money :}
( 1451520) send more + money = if ( 1)
( 1) cr s e n d m o r y .solution
( 1) then ( 1451520)
( 1451520) >try ( 4838400) ( 483840)
( 483840) >try ( 1209600) ( 120960)
( 120960) >try ( 241920) ( 24192)
( 24192) >try ( 40320) ( 4032)
( 4032) >try ( 5760) ( 576)
( 576) >try ( 720) ( 72)
( 72) then ( 81) >try ( 90) ( 9)
( 9) then ( 10) >try ( 10) ( 1) ;

- anton
--
M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
New standard: https://forth-standard.org/
EuroForth 2022: https://euro.theforth.net

Re: SEND+MORE=MONEY

<2023Feb9.115420@mips.complang.tuwien.ac.at>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=22305&group=comp.lang.forth#22305

  copy link   Newsgroups: comp.lang.forth
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: ant...@mips.complang.tuwien.ac.at (Anton Ertl)
Newsgroups: comp.lang.forth
Subject: Re: SEND+MORE=MONEY
Date: Thu, 09 Feb 2023 10:54:20 GMT
Organization: Institut fuer Computersprachen, Technische Universitaet Wien
Lines: 26
Message-ID: <2023Feb9.115420@mips.complang.tuwien.ac.at>
References: <78cfcd0a-5bc4-4584-8a92-9e8005a0e306n@googlegroups.com> <87ttzvzmmb.fsf@nightsong.com> <2023Feb9.084758@mips.complang.tuwien.ac.at> <138b28de-6f2a-449b-9279-629f3c17a03bn@googlegroups.com>
Injection-Info: reader01.eternal-september.org; posting-host="4a23064dba6cc3b448d0d7b13f318f12";
logging-data="700506"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19oeBT4733hRTesf+Wm2AXk"
Cancel-Lock: sha1:8Cia8MSCU8wLRtAoX40Uqr13w38=
X-newsreader: xrn 10.11
 by: Anton Ertl - Thu, 9 Feb 2023 10:54 UTC

"minf...@arcor.de" <minforth@arcor.de> writes:
>Anton Ertl schrieb am Donnerstag, 9. Februar 2023 um 09:32:34 UTC+1:
>> My guess is that the locals in ALLDIFFERENT play a large role in the
>> performance.
>
>Put the 8 digits in global values and at least they have not to be copied so
>many times around. IMO here we see a price ( implementation differences
>put aside ) to be paid for Forth locals that have to be moved away from
>the data stack.

The lxf result demonstrates that locals can be implemented much more
efficiently than in VFX or in SwiftForth. Maybe global variables work
faster on these systems (probably even lxf), but are not a good
solution for larger or reentrant programs.

Plus, in a really good native-code Forth system (beyond what current
Forth systems do), locals will also be more efficient, because they
can be kept in registers, while global variables have to be stored
into memory.

- anton
--
M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
New standard: https://forth-standard.org/
EuroForth 2022: https://euro.theforth.net

Re: SEND+MORE=MONEY

<cc10fb6f-e6fc-4592-8f46-98e9cf4134f4n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=22306&group=comp.lang.forth#22306

  copy link   Newsgroups: comp.lang.forth
X-Received: by 2002:ac8:5bd5:0:b0:3ba:138f:7b3f with SMTP id b21-20020ac85bd5000000b003ba138f7b3fmr2117354qtb.376.1675941775738;
Thu, 09 Feb 2023 03:22:55 -0800 (PST)
X-Received: by 2002:a0d:c442:0:b0:527:b471:cac7 with SMTP id
g63-20020a0dc442000000b00527b471cac7mr1194871ywd.489.1675941775413; Thu, 09
Feb 2023 03:22:55 -0800 (PST)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.forth
Date: Thu, 9 Feb 2023 03:22:55 -0800 (PST)
In-Reply-To: <78cfcd0a-5bc4-4584-8a92-9e8005a0e306n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=2a00:23c5:6f05:3a01:5913:d0dc:bea6:d19a;
posting-account=9A5f7goAAAD_QfJPZnlK3Xq_UhzYjdP-
NNTP-Posting-Host: 2a00:23c5:6f05:3a01:5913:d0dc:bea6:d19a
References: <78cfcd0a-5bc4-4584-8a92-9e8005a0e306n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <cc10fb6f-e6fc-4592-8f46-98e9cf4134f4n@googlegroups.com>
Subject: Re: SEND+MORE=MONEY
From: november...@gmail.com (NN)
Injection-Date: Thu, 09 Feb 2023 11:22:55 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 2233
 by: NN - Thu, 9 Feb 2023 11:22 UTC

Another solution ...

marker puzzle1

0 value s
0 value e
0 value n
0 value d
0 value m
0 value o
0 value r
0 value y

: send ( -- n ) d n e s 10 * + 10 * + 10 * + ;
: more ( -- n ) e r o m 10 * + 10 * + 10 * + ;
: money ( -- n ) y e n o m 10 * + 10 * + 10 * + 10 * + ;
: test ( -- f )
send more + money = ;

: all-unique { a u -- f }
begin u 1 > while
a 1+ u 1- a 1 search if 2drop false exit else 2drop then
a 1+ to a u 1- to u
repeat
true ;

: disp ( -- )
cr ." s e n d " s . e . n . d .
cr ." m o r e " m . o . r . e .
cr ." m o n e y " m . o . n . e . y .
cr ;

: dig ( a1 -- a2 n ) dup 1+ swap c@ 48 - ;

: start ( -- )
cr ." Solving... " cr
99999999 1 do
i s>d <# # # # # # # # # #> drop
dig to s
dig to e
dig to n
dig to d
dig to o
dig to r
dig to y
dig to m
drop
i s>d <# # # # # # # # # #> all-unique if
m 0<> if
test if
cr ." success" cr disp
then
then
then
loop ;

bye

Re: SEND+MORE=MONEY

<7df53e6f-b86c-49e2-ad41-e8debb3b3630n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=22307&group=comp.lang.forth#22307

  copy link   Newsgroups: comp.lang.forth
X-Received: by 2002:a37:b284:0:b0:727:8aee:c5a5 with SMTP id b126-20020a37b284000000b007278aeec5a5mr991398qkf.321.1675946821224;
Thu, 09 Feb 2023 04:47:01 -0800 (PST)
X-Received: by 2002:a0d:db0e:0:b0:52e:c5af:c376 with SMTP id
d14-20020a0ddb0e000000b0052ec5afc376mr1747ywe.391.1675946820937; Thu, 09 Feb
2023 04:47:00 -0800 (PST)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.forth
Date: Thu, 9 Feb 2023 04:47:00 -0800 (PST)
In-Reply-To: <7f3b51ea-1680-4d80-890b-28ac718d288fn@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=154.121.48.163; posting-account=KJSw4AoAAACRkUCek5r_78mFj6sHzH4C
NNTP-Posting-Host: 154.121.48.163
References: <78cfcd0a-5bc4-4584-8a92-9e8005a0e306n@googlegroups.com>
<787c9189-9528-4a55-8462-8e924c68ff7cn@googlegroups.com> <7f3b51ea-1680-4d80-890b-28ac718d288fn@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <7df53e6f-b86c-49e2-ad41-e8debb3b3630n@googlegroups.com>
Subject: Re: SEND+MORE=MONEY
From: ahmed.me...@univ-bejaia.dz (Ahmed MELAHI)
Injection-Date: Thu, 09 Feb 2023 12:47:01 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 6474
 by: Ahmed MELAHI - Thu, 9 Feb 2023 12:47 UTC

Le jeudi 9 février 2023 à 08:13:20 UTC, minf...@arcor.de a écrit :
> Ahmed MELAHI schrieb am Donnerstag, 9. Februar 2023 um 09:00:14 UTC+1:
> > > SENDMOREMONEY
> > Hi everybody,
> > Here is a program that gives all possible solutions (there are 25), written in gforth.
> Thanks! 25 solutions appear when M is allowed to be zero.
> Even more solutions appear when the all-digits-different constraint is taken out.
>
> I am fascinated by those many completely different approaches in this thread.
Hi,
For the case of m<>0 (so m must be 1), the same program gives the also the unique result, by selecting the solution with m=1.
To get the unique result directly,one can set 1 to m, and search for the others.
Here is the program (the same as the previous, with some changes)

s" random.fs" included

: not 0= ;

0 value s
0 value e
0 value n
0 value d
0 value m
0 value o
0 value r
0 value y

: send s 10 * e + 10 * n + 10 * d + ;
: more m 10 * o + 10 * r + 10 * e + ;
: money m 10 * o + 10 * n + 10 * e + 10 * y + ;

: is_send+more=money_? send more + money - 0= ;

create flags_chosen_vals 10 allot
flags_chosen_vals 10 erase
create flags_chosen_sendmory 8 allot
flags_chosen_sendmory 8 erase

: choose 10 random ;
: chosen flags_chosen_vals + 1 swap c! ;
: chosen_? flags_chosen_vals + c@ ;

: chosen_var_set flags_chosen_sendmory + 1 swap c! ;
: chosen_var_get flags_chosen_sendmory + c@ ;

: chosen--> dup chosen ;

: chosen_vars_init flags_chosen_sendmory 8 erase ;
: chosen_vals_init flags_chosen_vals 10 erase ;

: to_s choose chosen--> to s 0 chosen_var_set ;
: to_e choose dup chosen_? if drop else chosen--> to e 1 chosen_var_set then ;
: to_n choose dup chosen_? if drop else chosen--> to n 2 chosen_var_set then ;
: to_d choose dup chosen_? if drop else chosen--> to d 3 chosen_var_set then ;
: to_m choose dup chosen_? if drop else chosen--> to m 4 chosen_var_set then ;
: to_o choose dup chosen_? if drop else chosen--> to o 5 chosen_var_set then ;
: to_r choose dup chosen_? if drop else chosen--> to r 6 chosen_var_set then ;
: to_y choose dup chosen_? if drop else chosen--> to y 7 chosen_var_set then ;
: gen_sendmory
chosen_vals_init
1 chosen \ mark digit 1 as already chosen
chosen_vars_init
4 chosen_var_set \ mark m as already chosen
1 to m \ and set to 1

to_s
begin 1 chosen_var_get not while to_e repeat
begin 2 chosen_var_get not while to_n repeat
begin 3 chosen_var_get not while to_d repeat
\ begin 4 chosen_var_get not while to_m repeat
begin 5 chosen_var_get not while to_o repeat
begin 6 chosen_var_get not while to_r repeat
begin 7 chosen_var_get not while to_y repeat
;

: .sendmory s . e . n . d . m . o . r . y . ;

: sendmory_as_number s 10 * e + 10 * n + 10 * d + 10 * m + 10 * o + 10 * r + 10 * y + ;

1000 value max_results_size
create results max_results_size cells allot
0 value result_counter

: to_results
sendmory_as_number
result_counter 0 ?do
dup i cells results + @ = if
drop unloop exit
then
loop
result_counter cells results + !
result_counter 1+ to result_counter
result_counter cr . ." solutions found"
;

: go_sendmoremoney
cr
." solving ..."
0 to result_counter
0 do
gen_sendmory
is_send+more=money_? if
( cr .sendmory)
to_results
unloop exit
then
loop
cr cr result_counter . ." solutions found"
;

: .send send 4 .r ;
: .more more 4 .r ;
: .money money 5 .r ;

: to_sendmory
10000000 /mod to s
1000000 /mod to e
100000 /mod to n
10000 /mod to d
1000 /mod to m
100 /mod to o
10 /mod to r
to y
;


: .send+more=money .send ." + " .more ." = " .money ;

: .results
." sol_num sendmory send + more = money"
result_counter 0 ?do
cr i 7 .r 3 spaces
i cells results + @ dup
. 3 spaces
to_sendmory .send+more=money
loop
;

: .unique_result
cr ." The unique solution is:" cr
cr
." send + more = money"
cr
result_counter 0 ?do
i cells results + @ dup
to_sendmory
m 1 = if
.send+more=money
unloop
exit
then
loop
;

: go go_sendmoremoney cr .unique_result ;

10000000 go

bye

Pages:12345
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor