Hi,
Here, 3 programs are presented:
-1- send+more=money: without using permutations, use all_diff defined here in the program, also note the definition of (sendmoremoney), it has the form of declarative constrained logic, using the words defined at the begining of the program. this program take a while to find the solution (due to absence of permutations).
-2- send+more=money: with use of permutations, also note the form of the word (sendmoremoney), it uses declarative constained logic using the words defined in the begining of the program. this program find the solution rapidly.
-3- to+go=out: as the first program, here there are just 4 unknowns. it find the solution rapidly.
N.B. the words defined at the begining can be moved to another program (module) and included in the application program.
First program:
s" random.fs" included
: :- 1 ;
: , and dup 0= if exit then ;
: min_max 1 -rot over - 1+ random + ;
100 value diff_flags_max_size
create all_diff_flags diff_flags_max_size allot
0 value nvars
: all_diff_flags_init all_diff_flags diff_flags_max_size erase ;
: all_diff
all_diff_flags_init
nvars 0 do
all_diff_flags + dup c@ if
nvars i - 0 do
drop
loop
0 unloop exit
else
1 swap c!
then
loop
1
;
: -: and if 1 cr ." solution found" else 0 then ;
\ here begins the application to send+more=money
8 to nvars
0 value s
0 value e
0 value n
0 value d
0 value m
0 value o
0 value r
0 value y
: (sendmoremoney)
:- 0 9 min_max to s , 0 9 min_max to e , 0 9 min_max to n , 0 9 min_max to d ,
0 9 min_max to m , 0 9 min_max to o , 0 9 min_max to r , 0 9 min_max to y ,
s e n d m o r y all_diff ,
m 0> ,
s 10 * e + 10 * n + 10 * d +
m 10 * o + 10 * r + 10 * e + +
m 10 * o + 10 * n + 10 * e + 10 * y + = -:
;
: sendmoremoney
cr ." Solving ..." cr
0 do
(sendmoremoney)
if 1 unloop exit else then
loop
0
;
: .solution
if
cr
." the solution is: "
cr
s 10 * e + 10 * n + 10 * d + 4 .r ." + "
m 10 * o + 10 * r + 10 * e + 4 .r ." = "
m 10 * o + 10 * n + 10 * e + 10 * y + 5 .r
else
cr
." no solution found"
then
cr
;
: go sendmoremoney .solution ;
\ 100000000 go
utime 100000000 go utime d- dnegate d>f 1e-6 f* cr ." Done in: " f. ." seconds."
Second program:
s" random.fs" included
: :- 1 ;
: , and dup 0= if exit then ;
: min_max 1 -rot over - 1+ random + ;
100 value diff_flags_max_size
create all_diff_flags diff_flags_max_size allot
0 value nvars
: all_diff_flags_init all_diff_flags diff_flags_max_size erase ;
: all_diff
all_diff_flags_init
nvars 0 do
all_diff_flags + dup c@ if
nvars i - 0 do
drop
loop
0 unloop exit
else
1 swap c!
then
loop
1
;
: -: and if 1 cr ." solution found" else 0 then ;
\ here begins the application to send+more=money
8 to nvars
0 value s
0 value e
0 value n
0 value d
0 value m
0 value o
0 value r
0 value y
\ s e n d m o r y : the 8 first positions
create vals 0 c, 1 c, 2 c, 3 c, 4 c, 5 c, 6 c, 7 c, 8 c, 9 c,
0 value a
0 value b
: gen_new_permutation
\ by exchanging values in vals at a and b positions,
\ a and b are randomly chosen in 0, ..., 9.
10 random to a
10 random to b
vals a + c@
vals b + c@
vals a + c!
vals b + c!
;
: gen_sendmory
1
gen_new_permutation
7 for vals i + c@ next
to s to e to n to d to m to o to r to y
;
: (sendmoremoney)
:- gen_sendmory ,
m 0> ,
s 10 * e + 10 * n + 10 * d +
m 10 * o + 10 * r + 10 * e + +
m 10 * o + 10 * n + 10 * e + 10 * y + = -:
;
: sendmoremoney
cr ." Solving ..." cr
0 do
(sendmoremoney)
if 1 unloop exit else then
loop
0
;
: .solution
if
cr
." the solution is: "
cr
s 10 * e + 10 * n + 10 * d + 4 .r ." + "
m 10 * o + 10 * r + 10 * e + 4 .r ." = "
m 10 * o + 10 * n + 10 * e + 10 * y + 5 .r
else
cr
." no solution found"
then
cr
;
: go sendmoremoney .solution ;
\ 10000000 go
utime 10000000 go utime d- dnegate d>f 1e-6 f* cr ." Done in: " f. ." seconds."
Third program:
s" random.fs" included
: :- 1 ;
: , and dup 0= if exit then ;
: min_max 1 -rot over - 1+ random + ;
100 value diff_flags_max_size
create all_diff_flags diff_flags_max_size allot
0 value nvars
: all_diff_flags_init all_diff_flags diff_flags_max_size erase ;
: all_diff
all_diff_flags_init
nvars 0 do
all_diff_flags + dup c@ if
nvars i - 0 do
drop
loop
0 unloop exit
else
1 swap c!
then
loop
1
;
: -: and if 1 cr ." solution found" else 0 then ;
\ here begins the application to to+go=out
4 to nvars
0 value t
0 value o
0 value g
0 value u
: (togoout)
:- 0 9 min_max to t , 0 9 min_max to o , 0 9 min_max to g , 0 9 min_max to u ,
t o g u all_diff ,
o 0> ,
t 10 * o +
g 10 * o + +
o 10 * u + 10 * t + = -:
;
: togoout
cr ." Solving ..." cr
0 do
(togoout)
if 1 unloop exit else then
loop
0
;
: .solution
if
cr
." the solution is: "
cr
t 10 * o + 2 .r ." + "
g 10 * o + 2 .r ." = "
o 10 * u + 10 * t + 3 .r
else
cr
." no solution found"
then
cr
;
: go togoout .solution ;
\ 100000000 go
utime 100000000 go utime d- dnegate d>f 1e-6 f* cr ." Done in: " f. ." seconds."