Anonymous user
Sum and product puzzle: Difference between revisions
m
→version 5, fast: simplified some code, split a long line, optimized search.
Walterpachl (talk | contribs) m (→{{header|REXX}}: added wordsort as internal procedure) |
m (→version 5, fast: simplified some code, split a long line, optimized search.) |
||
Line 2,754:
===version 5, fast ===
This REXX version is over '''ten''' times faster than the previous REXX version.
<lang rexx>/*REXX program solves the Sum and Product Puzzle (also known as the Impossible Puzzle).*/
@.= 0;
do j=5 by 2 to H
do k=3
end /*k*/; @.j= 1 /*
end /*j*/
@.2=1 /*assign the even prime, ex post facto.*/
do s=2 for H-1; if C1(s)==0 then iterate /*find and display the puzzle solution.*/
Line 2,766 ⟶ 2,768:
if $>0 then say "The numbers are: " $ " and " s-$
end /*s*/
exit
/*──────────────────────────────────────────────────────────────────────────────────────*/
C1: procedure expose @.;
do a=2 for s%2-1; if @.a then do; _= s - a; if @._ then return 0;
end; /*a*/; return 1
/*──────────────────────────────────────────────────────────────────────────────────────*/
C2: procedure expose @. H; parse arg p; $= 0 /*validate the second puzzle condition.*/
do j=2 while j*j
if p//j==0 then do; q= p % j
if q>=2 then if q<=H then if C1(j+q) then if $ then return 0
|