|
Post by tsh73 on Oct 23, 2022 22:12:03 GMT
posted at LB forum, I repost it here for the benefit of folks who did not read LB forum. libertybasiccom.proboards.com/thread/2142/challenge-math-puzzle
This is the school textbook problem, 6th form It is supposed to be solved by pen and paper (done that) But How to make computer solve it? What data structures to use to represent problem? Looks rather challenging to me... 'There was a box of apples and pears 'Some are big, some are small; 'some are yellow, rest are green 'There are no small pears 'and no small green apples 'Some numbers are given: '25 Apples, 17 pears '32 big fruits '28 yellow ones. 'There are two more green apples then green pears. ' 'Find the number of big yellow apples
So. There is (25 Apples, 17 pears) = 42 fruits total 7 given restrictions How can one solve it?
So far we have a pen-and-paper approach programmed in LB and I got brute force working in JB (with nested loops). I'll post my code in a day or two so not spoil the fun for others. Any takers?
|
|
|
Post by plus on Oct 23, 2022 22:40:37 GMT
This reminds me of Einstein's Zebra Puzzle at Rosetta Code: rosettacode.org/wiki/Zebra_puzzleWould like to know how to code this systematically and not do all the permutations (if could do that).
|
|
|
Post by plus on Oct 24, 2022 9:51:42 GMT
So I worked out the logic like this:
' Apples and Pears Count Logic b+ 2022-10-24 4AM I woke up and started thinking about this :)
' Problem from tsh73 JB Forum https://justbasiccom.proboards.com/thread/905/math-puzzle-challenge 'There was a box of apples and pears 'Some are big, some are small; 'some are yellow, rest are green 'There are no small pears 'and no small green apples 'Some numbers are given: '25 Apples, 17 pears '32 big fruits '28 yellow ones. 'There are two more green apples then green pears. ' 'Find the number of big yellow apples '==================================================================================================== '3 pairs of mutually exclusive qualities 'A for Apple P for Pear, S for small L for Large, Y for Yellow G for green 'A + P = 42 total fruit '42 - 32L = 10 S '42 - 28Y = 14 G
'Summary A/P = 25/17 : S/L = 10/32 : Y/G = 28/14
' There are 8 combos of quality Fruit(2) * Size(2) * Color(2) = 8 'ASY 'ASG = 0 'no 'ALY > we want answer to this 'ALG 'PSY = 0 no SP's 'PSG = 0 no SP's 'PLY 'PLG
' ALG = PLG + 2 There are two more green apples then green pears. ' 14 G so PLG + PLG + 2 = 14 ' 2 * PLG = 12 ' PLG = 6 ' then ALG = 8
' Total Pears = 17 = PLY + PLG ' 17 = PLY + 6 ' 11 = PLY '
' Now we know this table of Apples and Pears:
' ASY = 10 all small fruit are apples ' ASG = 0 'no small green apples ' ALY > we want answer to this ' ALG = 8
' PSY = 0 no small pears ' PSG = 0 no small pears ' PLY = 11 because total Pears = 17 - PLG = PLY ' PLG = 6
' All Apples is 25 = ASY + ASG + ALY + ALG ' 25 = 10 + 0 + ALY + 8 ' 25 - 18 = ALY ' 7 = ALY
' check 'ASY = 10 all small fruit are apples 'ASG = 0 no small green apples 'ALY = 7 > we want answer to this 'ALG = 8 = 25 apples
'PSY = 0 no small pears 'PSG = 0 no small pears 'PLY = 11 'PLG = 6 = 17 Pears
|
|
|
Post by plus on Oct 25, 2022 18:36:03 GMT
OK I got the code to determine the ASY = 10, next need to show it Algebra Substitution, hmm... scratches head
' Apples and Pears 2 ' Problem from tsh73 JB Forum https://justbasiccom.proboards.com/thread/905/math-puzzle-challenge 'There was a box of apples and pears 'Some are big, some are small; 'some are yellow, rest are green 'There are no small pears 'and no small green apples 'Some numbers are given: '25 Apples, 17 pears '32 big fruits '28 yellow ones. 'There are two more green apples then green pears. ' 'Find the number of big yellow apples '====================================================================================================
Dim names$(16), values(16) 'the 8 combos of Fruit(2) * Size(2) * Color(2) ' Use -1 to tell code that that value has not been determined yet names$(1) = "ASY" : values(1) = -1 ' for now names$(2) = "ASG" : values(2) = 0 ' stays 0 names$(3) = "ALY" : values(3) = -1 ' > we want answer to this names$(4) = "ALG" : values(4) = -1 ' for now names$(5) = "PSY" : values(5) = 0 ' stays = 0 no SP's names$(6) = "PSG" : values(6) = 0 ' stays = 0 no SP's names$(7) = "PLY" : values(7) = -1 ' for now names$(8) = "PLG" : values(8) = -1 ' for now
names$(9) = "A" : values(9) = 25 names$(10) = "P" : values(10) = 17 names$(11) = "L" : values(11) = 32 names$(12) = "Y" : values(12) = 28 names$(13) = "F" : values(13) = values(9) + values(10) names$(14) = "S" : values(14) = values(13) - values(11) names$(15) = "G" : values(15) = values(13) - values(12)
'Print sum("A"), sum("P") 'print sum("S"), sum("L") 'print sum("Y"), sum("G") 'print values(14), values(15)
Q$ = "APSLYG" ' qualities ' notice there are 3 or 4 values not = -1 for S so the 4th one = the sumS - the total values of others, simple algebra
[SolveAgain] sf = 0 for L = 1 to 6 test$ = Solve$(mid$(Q$, L, 1)) If test$ <> "" then print test$ : sf = 1 next if sf = 1 then goto [SolveAgain] print "Finished"
' alg + plg = g alg = plg + 2 Next up teach code to do this little algebra problem
' plg + 2 + plg = g
function Solve$(letter$) ' return yes or no for N = 1 to 8 if instr(names$(N), letter$)> 0 then if values(N) = -1 then count = count + 1 : saveN = N else total = total + values(N) end if end if next if count = 1 then ' only one value not established yet we can figure it's value for j = 9 to 15 if names$(j) = letter$ then ' find the letter and it's value, subtract total of others values(saveN) = values(j) - total ' this fills in a missing value!!! Solve$ = names$(saveN) + " = " + str$(values(saveN)) exit function end if next end if end function
function sum(letter$) for N = 1 to 8 if instr(names$(N), letter$) then sum = sum + values(N) next end function
'3 pairs of mutually exclusive qualities 'A for Apple P for Pear, S for small L for Large, Y for Yellow G for green 'A + P = 42 total fruit '42 - 32L = 10 S '42 - 28Y = 14 G
'Summary ' A/P = 25/17 : S/L = 10/32 : Y/G = 28/14
' There are 8 combos of quality Fruit(2) * Size(2) * Color(2) = 8 'ASY 'ASG = 0 'no 'ALY > we want answer to this 'ALG 'PSY = 0 no SP's 'PSG = 0 no SP's 'PLY 'PLG
' ALG = PLG + 2 There are two more green apples then green pears. ' 14 G so PLG + PLG + 2 = 14 ' 2 * PLG = 12 ' PLG = 6 ' then ALG = 8
' Total Pears = 17 = PLY + PLG ' 17 = PLY + 6 ' 11 = PLY '
' Now we know this table of Apples and Pears:
' ASY = 10 all small fruit are apples ' ASG = 0 'no small green apples ' ALY > we want answer to this ' ALG = 8
' PSY = 0 no small pears ' PSG = 0 no small pears ' PLY = 11 because total Pears = 17 - PLG = PLY ' PLG = 6
' All Apples is 25 = ASY + ASG + ALY + ALG ' 25 = 10 + 0 + ALY + 8 ' 25 - 18 = ALY ' 7 = ALY
' check 'ASY = 10 all small fruit are apples 'ASG = 0 no small green apples 'ALY = 7 > we want answer to this 'ALG = 8 ' = 25 apples
'PSY = 0 no small pears 'PSG = 0 no small pears 'PLY = 11 'PLG = 6 ' = 17 Pears
It seems allot of work to try and solve it with computer code but imagine having to solve 1000's of similar problems eg changing the numbers, adding more qualities like Zebra Problem.
|
|
|
Post by plus on Oct 25, 2022 19:24:45 GMT
OK I just did the algebra and converted it to variable values in a last variable definition. The code solves remaining 4 variable values. Fortunately it so happens to be enough information to solve for all fruit!
' Apples and Pears 2 ' Problem from tsh73 JB Forum https://justbasiccom.proboards.com/thread/905/math-puzzle-challenge 'There was a box of apples and pears 'Some are big, some are small; 'some are yellow, rest are green 'There are no small pears 'and no small green apples 'Some numbers are given: '25 Apples, 17 pears '32 big fruits '28 yellow ones. 'There are two more green apples then green pears. ' 'Find the number of big yellow apples '====================================================================================================
Dim names$(16), values(16) 'the 8 combos of Fruit(2) * Size(2) * Color(2) ' Use -1 to tell code that that value has not been determined yet names$(1) = "ASY" : values(1) = -1 ' for now names$(2) = "ASG" : values(2) = 0 ' stays 0 names$(3) = "ALY" : values(3) = -1 ' > we want answer to this names$(4) = "ALG" : values(4) = -1 ' for now names$(5) = "PSY" : values(5) = 0 ' stays = 0 no SP's names$(6) = "PSG" : values(6) = 0 ' stays = 0 no SP's names$(7) = "PLY" : values(7) = -1 ' for now names$(8) = "PLG" : values(8) = -1 ' for now
names$(9) = "A" : values(9) = 25 names$(10) = "P" : values(10) = 17 names$(11) = "L" : values(11) = 32 names$(12) = "Y" : values(12) = 28 names$(13) = "F" : values(13) = values(9) + values(10) names$(14) = "S" : values(14) = values(13) - values(11) names$(15) = "G" : values(15) = values(13) - values(12)
'There are two more green apples then green pears ' values(15) is total of green fruit no small fruit is green so total green fruit is total of L ' value(15) = alg + plg ' values(15) = (plg + 2) + plg 'There are two more green apples then green pears ' values(15) = 2 * plg + 2 ' plg = (values(15) - 2)/2 ' plg = values(8) values(8) = (values(15) - 2) / 2 ' this is num
' early checking of code, turns out I never needed sum(Letter$) 'Print sum("A"), sum("P") 'print sum("S"), sum("L") 'print sum("Y"), sum("G") 'print values(14), values(15)
Q$ = "APSLYG" ' qualities ' notice there are 3 or 4 values not = -1 for S so the 4th one = the sumS - the total values of others, simple algebra
[SolveAgain] sf = 0 for L = 1 to 6 test$ = Solve$(mid$(Q$, L, 1)) If test$ <> "" then print "Solved! " + test$ : sf = 1 next if sf = 1 then goto [SolveAgain] print "Finished Solving" print print " OK this is breakdown of all fruit:" print " Sum of Apples is ";sum("A") print " Sum of Pears is ";sum("P") print " Sum of Large is ";sum("L") print " Sum of Small is ";sum("S") print " Sum of Green is ";sum("G") print " Sum of Yellow is ";sum("Y") print print " Each type:" for i = 1 to 8 print " ";names$(i);" = ";values(i) next
function Solve$(letter$) for N = 1 to 8 if instr(names$(N), letter$)> 0 then if values(N) = -1 then count = count + 1 : saveN = N else total = total + values(N) end if end if next if count = 1 then ' only one value not established yet we can figure it's value for j = 9 to 15 if names$(j) = letter$ then ' find the letter and it's value, subtract total of others values(saveN) = values(j) - total ' this fills in a missing value!!! Solve$ = names$(saveN) + " = " + str$(values(saveN)) exit function end if next end if end function
' never used but helped me build Solve from it! function sum(letter$) for N = 1 to 8 if instr(names$(N), letter$) then sum = sum + values(N) next end function
' from earlier post
'3 pairs of mutually exclusive qualities 'A for Apple P for Pear, S for small L for Large, Y for Yellow G for green 'A + P = 42 total fruit '42 - 32L = 10 S '42 - 28Y = 14 G
'Summary ' A/P = 25/17 : S/L = 10/32 : Y/G = 28/14
' There are 8 combos of quality Fruit(2) * Size(2) * Color(2) = 8 'ASY 'ASG = 0 'no 'ALY > we want answer to this 'ALG 'PSY = 0 no SP's 'PSG = 0 no SP's 'PLY 'PLG
' ALG = PLG + 2 There are two more green apples then green pears. ' 14 G so PLG + PLG + 2 = 14 ' 2 * PLG = 12 ' PLG = 6 ' then ALG = 8
' Total Pears = 17 = PLY + PLG ' 17 = PLY + 6 ' 11 = PLY '
' Now we know this table of Apples and Pears:
' ASY = 10 all small fruit are apples ' ASG = 0 'no small green apples ' ALY > we want answer to this ' ALG = 8
' PSY = 0 no small pears ' PSG = 0 no small pears ' PLY = 11 because total Pears = 17 - PLG = PLY ' PLG = 6
' All Apples is 25 = ASY + ASG + ALY + ALG ' 25 = 10 + 0 + ALY + 8 ' 25 - 18 = ALY ' 7 = ALY
' check 'ASY = 10 all small fruit are apples 'ASG = 0 no small green apples 'ALY = 7 > we want answer to this 'ALG = 8 ' = 25 apples
'PSY = 0 no small pears 'PSG = 0 no small pears 'PLY = 11 'PLG = 6 ' = 17 Pears
Output:
Solved! PLY = 11 Solved! ASY = 10 Solved! ALY = 7 Solved! ALG = 8 Finished Solving
OK this is breakdown of all fruit: Sum of Apples is 25 Sum of Pears is 17 Sum of Large is 32 Sum of Small is 10 Sum of Green is 14 Sum of Yellow is 28
Each type: ASY = 10 ASG = 0 ALY = 7 ALG = 8 PSY = 0 PSG = 0 PLY = 11 PLG = 6
|
|
|
Post by plus on Oct 26, 2022 13:34:05 GMT
OK I will try to explain how the Solve$(Letter$) function works.
Each letter A, P, L, S, Y, G is contained 4 times in the 8 groups of individual quantities: names$(1) = "ASY" : values(1) = -1 ' for now names$(2) = "ASG" : values(2) = 0 ' stays 0 names$(3) = "ALY" : values(3) = -1 ' > we want answer to this names$(4) = "ALG" : values(4) = -1 ' for now names$(5) = "PSY" : values(5) = 0 ' stays = 0 no SP's names$(6) = "PSG" : values(6) = 0 ' stays = 0 no SP's names$(7) = "PLY" : values(7) = -1 ' for now names$(8) = "PLG" : values(8) = -1 ' for now
For example A sum = 17 equals ASY + ASG + ALY + ALG
A single letter sum is total of 4 of individual 3-letter quantities.
Now we have all the single letter sums in next group of lets, plus the total of all Fruit, F: names$(9) = "A" : values(9) = 25 names$(10) = "P" : values(10) = 17 names$(11) = "L" : values(11) = 32 names$(12) = "Y" : values(12) = 28 names$(13) = "F" : values(13) = values(9) + values(10) names$(14) = "S" : values(14) = values(13) - values(11) names$(15) = "G" : values(15) = values(13) - values(12)
If we have Sum and 3 of the 4 items that make up the sum Then we can solve for the 4th item eg S = ASG + ASY + PSG + PSY S = Total Fruit F - L fruit or 42 - 32 leaves 10 ASY is unknown, I used -1 ASG = 0 PSY = 0 PSG = 0 So 10 = unknown + 0 + 0 + 0 unknown = 10
Not a huge leap of faith there for sure but the others Sums work the same. If we know the Sum of a letter (single letter name) and 3 of 4 3-letter items that make up that sum we can solve for unknown 4th item = sum - total of three knowns.
That is what the Solve$ function does
function Solve$(letter$) for N = 1 to 8 if instr(names$(N), letter$)> 0 then if values(N) = -1 then count = count + 1 : saveN = N else total = total + values(N) end if end if next if count = 1 then ' only one value not established yet we can figure it's value for j = 9 to 15 if names$(j) = letter$ then ' find the letter and it's value, subtract total of others values(saveN) = values(j) - total ' this fills in a missing value!!! Solve$ = names$(saveN) + " = " + str$(values(saveN)) exit function end if next end if end function
This part
for N = 1 to 8 if instr(names$(N), letter$)> 0 then if values(N) = -1 then count = count + 1 : saveN = N else total = total + values(N) end if end if next counts unknows values with -1 (you can't have any negative amounts of fruit in this problem, so -1 designates an unknown). Meanwhile it totals the knowns for that letter.
If it turns out there is only one unknown, ie Count = 1 then the unknown is solvable by taking the letter sum and subtracting the total of the 3 of 4 3-letter groups known values it contains.
So I am actually using variables names and their values, the trick was to recognize the letter in the name so we can lookup the associated value of that name.
Sorry for all the edits, I am trying to make the wording clear and easier to follow.
|
|
|
Post by plus on Oct 26, 2022 16:05:24 GMT
Oh wow, this problem has inspired me to attempt to Algebraically solve an equation with 1 unknown ie c = a + 2 * b - 3 * x when a, b, c are known and x is the unknown. c -a -2*b = -3*x (c -a -2*b)/ -3 = x
Interesting!
|
|
|
Post by plus on Oct 27, 2022 4:02:17 GMT
Update: dug up this old code, justbasiccom.proboards.com/thread/482/handy-formula-saverstill like how well it works, thought it would be a snap to do Algebraic simplifications with help from this work. After all it does powers, trig functions, nested parenthesis... Ha! but the stuff you do so easily with pencil and paper, moving stuff left and right of = sign, isolating the unknown variable(s)... is just a tiny bit harder to code!
|
|
|
Post by tsh73 on Oct 28, 2022 8:33:49 GMT
bPlus, many thanks for taking time to explain it.
Most interesting for mee seems that order of equations is not important. If is is solvable, then at any time there should be at least one solvable equation Loop by L checks all equations and stops then on last round (all 6 equations) nothing was solved (== likely nothing left to solve).
|
|
|
Post by plus on Oct 28, 2022 17:07:39 GMT
I did not take time to scramble the order of equations but I did test the code to see if the loop was needed, no! It was all solved in first pass through. I really should have attempted to break that by scrambling the order and then seeing if loop was ever needed. My bet is not but perhaps in another similar problem it might be?
I am reminded of Gaussian elimination for system of linear equations, so nice to work with real application problems.
|
|
|
Post by tsh73 on Oct 28, 2022 19:29:07 GMT
This scramble Q$ = "YSLAPG" uses two passes. So loop is needed.
|
|
|
Post by plus on Oct 28, 2022 19:50:19 GMT
Dang I did it the hard way by scrambling the order of the first 8 equations, but Q$ is much smarter way to change order, nice!
I was just about to post that changing order of first 8 assignments didn't make a difference in number of loops needed to solve all. But really it is the order of letters in Q$ that sets the order of what is checked and solved.
|
|
|
Post by marshawn on Dec 21, 2022 9:14:13 GMT
how did I not see this post on October?
|
|
|
Post by plus on Dec 21, 2022 17:47:09 GMT
how did I not see this post on October? Well I hope you didn't miss Einstein's Zebra Puzzle from Rosetta Code, solving that was milestone for me. Never did get around to Algebraic simplification code...
|
|