How to resolve the algorithm Sum and product puzzle step by step in the ooRexx programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Sum and product puzzle step by step in the ooRexx programming language
Table of Contents
Problem Statement
Solve the "Impossible Puzzle": It can be hard to wrap one's head around what the three lines of dialog between S (the "sum guy") and P (the "product guy") convey about the values of X and Y. So for your convenience, here's a break-down: Terminology:
Your program can solve the puzzle by considering all possible pairs (X, Y) in the range 2 ≤ X < Y ≤ 98, and then successively eliminating candidates based on the three facts. It turns out only one solution remains! See the Python example for an implementation that uses this approach with a few optimizations.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Sum and product puzzle step by step in the ooRexx programming language
Source code in the oorexx programming language
all =.set~new
Call time 'R'
cnt.=0
do a=2 to 100
do b=a+1 to 100-2
p=a b
if a+b>100 then leave b
all~put(p)
prd=a*b
cnt.prd+=1
End
End
Say "There are" all~items "pairs where X+Y <=" max "(and X
spairs=.set~new
Do Until all~items=0
do p over all
d=decompositions(p)
If take Then
spairs=spairs~union(d)
dif=all~difference(d)
Leave
End
all=dif
end
Say "S starts with" spairs~items "possible pairs."
sProducts.=0
Do p over sPairs
Parse Var p x y
prod=x*y
sProducts.prod+=1
End
pPairs=.set~new
Do p over sPairs
Parse Var p xb yb
prod=xb*yb
If sProducts.prod=1 Then
pPairs~put(p)
End
Say "P then has" pPairs~items "possible pairs."
Sums.=0
Do p over pPairs
Parse Var p xc yc
sum=xc+yc
Sums.sum+=1
End
final=.set~new
Do p over pPairs
Parse Var p x y
sum=x+y
If Sums.sum=1 Then
final~put(p)
End
si=0
Do p Over final
si+=1
sol.si=p
End
Select
When final~items=1 Then Say "Answer:" sol.1
When final~items=0 Then Say "No possible answer."
Otherwise Do; Say final~items "possible answers:"
Do p over final
Say p
End
End
End
Say "Elapsed time:" time('E') "seconds"
Exit
decompositions: Procedure Expose cnt. take spairs
epairs=.set~new
Use Arg p
Parse Var p aa bb
s=aa+bb
take=1
Do xa=2 To s/2
ya=s-xa
pp=xa ya
epairs~put(pp)
prod=xa*ya
If cnt.prod=1 Then
take=0
End
return epairs
all =.set~new
Call time 'R'
cnt.=0
do a=2 to 100
do b=a+1 to 100-2
p=.pairs~new(a,b)
if p~sum>100 then leave b
all~put(p)
prd=p~prod
cnt.prd+=1
End
End
Say "There are" all~items "pairs where X+Y <=" max "(and X
spairs=.set~new
Do Until all~items=0
do p over all
d=decompositions(p)
If take Then
spairs=spairs~union(d)
dif=all~difference(d)
Leave
End
all=dif
end
Say "S starts with" spairs~items "possible pairs."
sProducts.=0
Do p over sPairs
prod=p~prod
sProducts.prod+=1
End
pPairs=.set~new
Do p over sPairs
prod=p~prod
If sProducts.prod=1 Then
pPairs~put(p)
End
Say "P then has" pPairs~items "possible pairs."
Sums.=0
Do p over pPairs
sum=p~sum
Sums.sum+=1
End
final=.set~new
Do p over pPairs
sum=p~sum
If Sums.sum=1 Then
final~put(p)
End
si=0
Do p Over final
si+=1
sol.si=p
End
Select
When final~items=1 Then Say "Answer:" sol.1~string
When final~items=0 Then Say "No possible answer."
Otherwise Do; Say final~items "possible answers:"
Do p over final
Say p~string
End
End
End
Say "Elapsed time:" time('E') "seconds"
Exit
decompositions: Procedure Expose cnt. take spairs
epairs=.set~new
Use Arg p
s=p~sum
take=1
Do xa=2 To s/2
ya=s-xa
pp=.pairs~new(xa,ya)
epairs~put(pp)
prod=pp~prod
If cnt.prod=1 Then
take=0
End
return epairs
::class pairs
::attribute a -- allow access to attribute
::attribute b -- allow access to attribute
::attribute sum -- allow access to attribute
::attribute prod -- allow access to attribute
-- only the strict equality form is needed for the collection classes,
::method "=="
expose a b
use strict arg other
return a == other~a & b == other~b
-- not needed to make the set difference work, but added for completeness
::method "\=="
expose a b
use strict arg other
return a \== other~a | b \== other~b
::method hashCode
expose hash
return hash
::method init -- create pair, calculate sum, product
-- and index (blank delimited values)
expose hash a b sum prod oid
use arg a, b
hash = a~hashCode~bitxor(b~hashCode) -- create hash value
sum =a+b -- sum
prod=a*b -- product
::method string -- this creates the string to be shown
expose a b
return "[x="||a",y="||b"]"
You may also check:How to resolve the algorithm Morse code step by step in the J programming language
You may also check:How to resolve the algorithm Dice game probabilities step by step in the Mathematica / Wolfram Language programming language
You may also check:How to resolve the algorithm Create an HTML table step by step in the J programming language
You may also check:How to resolve the algorithm Sleep step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Numbers with equal rises and falls step by step in the Fortran programming language