Indeed!
I ran the little program shown below.
With a "standard brute force" approach, it took 31 seconds (type=1).
With the extra constraints it took only 22 seconds (type=2).
EDM>d puzzle^permut(1)
526485+197485=723970
That took 31 seconds.
EDM>d puzzle^permut(2)
526485+197485=723970
That took 22 seconds.
EDM>
(Try do test^permut to see what the routine actually does...)
permut ; Ed de Moel ; permutations
q
;
do(head,tail) New i
If $Length(tail)=1 Do @[EMAIL PROTECTED]
Quit
For i=1:1:$Length(tail) Do
do(head_$Extract(tail,i),$Extract(tail,1,i-1)_$Extract(tail,i+1,$Length(tail)))
Quit
;
test ;
Set action="act^permut(head_tail)"
Do do("","abc")
Quit
;
act(txt) Write !,txt
Quit
;
puzzle(type) ; To solve the puzzle:
New action,e,h1,h2,i,list,x
;
;
Set h1=$Horolog,h1=h1*86400+$Piece(h1,",",2)
; D O N A L D
; + G E R A L D
; -------------
; R O B E R T
;
; First recognize the different letters:
; 123456789-123456789-
Set x="DONALD+GERALD=ROBERT"
Kill list
For i=1:1:$Length(x) Set e=$Extract(x,i) Set:e?1u list(e,i)=""
Set i="",list="" For Set i=$Order(list(i)) Quit:i="" Set list=list_i
;
Set:type=1 action="solve1^permut(head_tail)"
Set:type=2 action="solve2^permut(head_tail)"
Do do^permut("",1234567890)
Set h2=$Horolog,h2=h2*86400+$Piece(h2,",",2)
Write !,"That took ",h2-h1," seconds."
Quit
;
solve1(text) New e,i,p
Set e=$Translate(x,list,text) Xecute "If "_e_" Write !,e"
Quit
;
solve2(text) New e,i,p
Set e=$Translate(x,list,text)
; D in Donald may not be 0
Quit:'$Extract(e,1)
; G in Gerald may not be 0
Quit:'$Extract(e,8)
; T at the end must be even
Quit:13579[$Extract(e,20)
Xecute "If "_e_" Write !,e"
Quit
;
--
**************************************************************
This electronic mail transmission contains confidential and/or
privileged information intended only for the person(s) named.
Any use, distribution, copying or disclosure by another person
is strictly prohibited.
**************************************************************


|