Sum and product puzzle: Difference between revisions

m
→‎version 5, fast: changed some comments.
m (→‎version 5, fast: added a message if there wasn't a solution found.)
m (→‎version 5, fast: changed some comments.)
Line 2,756:
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; Hh= 100; @.3= 1 /*assign array default; assign high P.*/
do j=5 by 2 to Hh /*find all odd primes ≤ 1st argument.*/
do k=3 while k*k<=j; if j//k==0 then iterate j /*J ÷ by K ? */
end /*k*/; @.j= 1 /*found a net prime number: J */
end /*j*/
@.2=1 /*assign the even prime, ex post facto.*/
do s=2 for Hh-1; if C1(s)==0 then iterate /*find and display the puzzle solution.*/
$= 0; do m=2 for s%2 -1 /* [↓] check for uniqueness of product*/
if C2(m * (s-m)) then do; if $>0 then iterate s; $= m; end
Line 2,775:
end; /*a*/; return 1
/*──────────────────────────────────────────────────────────────────────────────────────*/
C2: procedure expose @. Hh; parse arg p; $= 0 /*validate the second puzzle condition.*/
do j=2 while j*j<p /*perform up to the square root of P. */
if p//j==0 then do; q= p % j
if q>=2 then if q<=Hh then if C1(j+q) then if $ then return 0
else $= 1
end
end /*j*/; return $</lang>
{{out|output|text=&nbsp; when using the default internal input:}}
<pre>
The numbers are: 4 and 13