Vietnamese snake puzzle - Closed
|
05-21-2015, 01:51 PM
(This post was last modified: 05-21-2015 01:52 PM by Tugdual.)
Post: #21
|
|||
|
|||
RE: Vietnamese snake puzzle - Closed
(05-21-2015 01:18 PM)fhub Wrote:Thanks! I'm getting used to the 50g CAS and tend to forget about wandering floats.(05-21-2015 11:53 AM)Tugdual Wrote: I think there are 128 in my list and this is what Haskell returned.Yes, I do expect more. Code: [x | x <- permutations [1..9], abs(x!!0+13*x!!1/x!!2+x!!3+12*x!!4-x!!5-11+x!!6*x!!7/x!!8-10-66) < 1E-6] |
|||
05-22-2015, 04:42 AM
Post: #22
|
|||
|
|||
RE: Vietnamese snake puzzle - Closed
Pleased to say we ended up with all (?) the solutions.
Did we gain any insight? As far as I can see, just that there's more than one way to stuff a snake. |
|||
05-22-2015, 12:04 PM
(This post was last modified: 05-22-2015 12:19 PM by Tugdual.)
Post: #23
|
|||
|
|||
RE: Vietnamese snake puzzle - Closed
(05-22-2015 04:42 AM)Gerald H Wrote: Pleased to say we ended up with all (?) the solutions.I don't think so. Having had a look at the equation, I just see a 9 variables ordinary equation and nothing tinkles my brain. Basically you zero this: \[\frac { 13*b*i+c*g*h }{ ci } +12*e+a+d-f-87\] Note: I think the guardian agrees with the ordinary aspect: "There are some puzzles that you solve with a flash of insight, and some others – like this one – where there is no alternative but trial and error." |
|||
05-22-2015, 08:00 PM
(This post was last modified: 05-23-2015 06:36 PM by Gerson W. Barbosa.)
Post: #24
|
|||
|
|||
RE: Vietnamese snake puzzle - Closed
An RPL solution, basically what I did by hand, except that I was a bit smarter and didn't go through some impossible alternatives. But the hp 50g is much faster than I am, so no problem: less than four minutes to find four solutions which can be expanded to more by applying commutative properties of multiplication and addition (9 seconds on the emulator). Writing the program took a bit longer, however, no to mention I lost more the more complicated inner structers last night, by accidentally replacing it with something else. No backups, no notes as it had been done right on the calculator only.
Conclusion: better do this by hand :-) Code:
{ [ 9. 4. 1. 5. 2. 7. 3. 8. 6. ] [ 3. 2. 1. 5. 4. 7. 8. 9. 6. ] [ 7. 3. 1. 5. 2. 6. 8. 9. 4. ] [ 6. 3. 1. 9. 2. 5. 7. 8. 4. ] } This should work on the HP-48G also, only remember to make this changes and others I may have forgotten: NIP: SWAP DROP DUPDUP: DUP DUP PICK3: 3 PICK Gerson. Edited to fix the range of the loops. I had found an incredible 50-second runtime but the outer loops were ranging from 2 to 6, 2 to 4 and 2 to 8, respectively, and yet the program returned the same four solutions. Something to be explored and analyzed to improve the program. There is still room for improvement such as better list handling, for instance. P.S.: Considering our equation is a + 13*b + d + 12*e - f - 11 + g*h/i = 76 We can write the following table: b e f| result -----+------- 9 x 8| > 109 8 x 9| > 95 7 2 9| > 106 6 2 9| > 90 => b < 6 x 9 8| > 100 x 8 9| > 87 => e < 8 5 7 9| > 140 5 6 9| > 120 4 5 9| > 106 3 5 9| > 90 => e < 5 Now we can safely define the ranges of the outer loops and be sure we won't miss any solution: Code:
{ [ 9. 4. 1. 5. 2. 7. 3. 8. 6. ] [ 3. 2. 1. 5. 4. 7. 8. 9. 6. ] [ 7. 3. 1. 5. 2. 6. 8. 9. 4. ] [ 6. 3. 1. 9. 2. 5. 7. 8. 4. ] } 33.6 seconds on the physical HP 50g! This matches Tugdual's results when c=1, considering all valid permutations: 941527386 941527836 541927836 541927386 321547896 321547986 521347986 521347896 731526894 731526984 531726984 531726894 631925874 631925784 931625874 931625784 |
|||
05-22-2015, 10:21 PM
Post: #25
|
|||
|
|||
RE: Vietnamese snake puzzle - Closed
(05-22-2015 08:00 PM)Gerson W. Barbosa Wrote: An RPL solution, basically what I did by hand, except that I was a bit smarter and didn't go through some impossible alternatives. But the hp 50g is much faster than I am, so no problem: less than four minutes to find four solutions which can be expanded to more by applying commutative properties of multiplication and addition (9 seconds on the emulator). Writing the program took a bit longer, however, no to mention I lost more the more complicated inner structers last night, by accidentally replacing it with something else. No backups, no notes as it had been done right on the calculator only.Problem is hat your assumption c==1 is not valid. You really need to consider the 2 fractions as a whole instead of 2 individual fractions that deliver integers. |
|||
05-22-2015, 11:32 PM
Post: #26
|
|||
|
|||
RE: Vietnamese snake puzzle - Closed
(05-22-2015 10:21 PM)Tugdual Wrote: Problem is hat your assumption c==1 is not valid. You really need to consider the 2 fractions as a whole instead of 2 individual fractions that deliver integers. Hi, Tugdual, It was more a choice rather than an assumption. Since this problem was set up for eight-year old children, or so they say, I thought solutions involving fractional intermediate results might have been avoided. In that case, 'c' has necessarily to be 1. Also, I was only interested in finding at least one solution -- and this can be done by hand. Of course, the more complex problem of finding all possible solutions does require a calculating device, better if the task is accomplished in an elegant and compact fashion like you did. Years ago, Karl Schneiner posted another interesting problem involving nine digits without repetions and fractions: http://www.hpmuseum.org/cgi-sys/cgiwrap/...ead=112157 But please ignore my ugly brute force attemps there, except perhaps the Monte-Carlo approach at the end, despite it being extremely inefficient. Gerson. |
|||
05-23-2015, 09:08 AM
Post: #27
|
|||
|
|||
RE: Vietnamese snake puzzle - Closed
Gerson: for the intermediate results to be integer, c must divide b, which is not the same as saying c must be 1 ;-))
I've written an RPL solution as well that, on EMU48 at full pelt, cranks out all 136 solutions in 30 seconds. But I'm not happy with it yet. I used Heap's algorithm to generate all permutations - then it took 256 seconds. Including the condition that Code: MOD(13*b*i + g*h*c,c*i)=0 Cheers, Werner 41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE |
|||
05-24-2015, 03:14 AM
(This post was last modified: 05-24-2015 03:16 AM by Gerson W. Barbosa.)
Post: #28
|
|||
|
|||
RE: Vietnamese snake puzzle - Closed
(05-23-2015 09:08 AM)Werner Wrote: Gerson: for the intermediate results to be integer, c must divide b, which is not the same as saying c must be 1 ;-)) You're right. Anyway, by choosing c = 1 I was able to find 16 solutions in 30 seconds on a physical HP 50g (see my edited post above), better than only one which was my goal in the first place. But I recognize I shouldn't be lazy and should try to find all 136 solutions in a reasonable time. Perhaps one of these days I will :-) Cheers, Gerson. |
|||
05-24-2015, 06:40 PM
(This post was last modified: 05-25-2015 06:37 AM by Werner.)
Post: #29
|
|||
|
|||
RE: Vietnamese snake puzzle - Closed
My RPL solution to the arithmetic puzzle (long):
all timings are for EMU48 on my laptop. It's about 120 times faster than a real 48GX. First, generate permutations: The following routine will accept a list L on level 2 and an executable object ob in level 1, and will apply ob to all permutations of the list L. All result objects (from executing ob), if any, will be wrapped up in an output list. It does not use Heap's algorithm, for two reasons: - Heap's algorithm uses a single object swap between permutations. But a swap in RPL is a ROLL and a ROLLD, and it can be done with just a single ROLL. - at every level, the order at the beginning and end of the level are the same - which is not the case for Heap's algorithm Code: @ DOPERM1 To simply generate all permutations in a list, for instance, the following ob can be used: Code: @ l1 .. lN -> { l1 .. lN } l1 .. lN Code: { 1 2 3 } Code: { { 2 1 3 } { 2 3 1 } { 3 2 1 } { 3 1 2 } { 1 3 2 } { 1 2 3 } } and listing all solutions to the arithmetic challenge can be done like this: Code: \<< 427 seconds, 136 solutions, 9! = 362880 permutations tested We may speed this up unrolling inner loops and so, but we're still testing 362880 permutations. We had better find ways to cut this number down. Suppose we find B C G H I first, an test whether (13*B*I + G*H*C)/(C*I) is an integer. If it isn't, there's no need to further test all permutations of A D E and F. We can do this if we change DOPERM1 as follows: Code: @ DOPERM2 This time, the simple permutation list generating 'ob' looks like this: Code: \<< and the ob for straightforward generation of all solutions like this: Code: \<< The execution now takes even longer: 495 seconds This time, however, we can cut the permutations short: once we selected 5 numbers (when permutation 'level' equals 4), we can perform our test, as follows: Code: \<< We can check C or I are not 5 or 7: Code: \<< We can unroll the level-4 permutations. Since A and D are commutable, we need generate only 8 permutations (the bottom 2 need not be swapped if those are A and D). At the same time, we can re-use the already calculated amount (13*B*I + G*H*C)/(C*I) Also, let's output the solutions in ABCDEFGHI order again: Code: \<< We can roughly halve that number again by generating the 36 (G,H) combinations first and permuting the 7 other numbers Code: \<< To be complete, here's a version to find all solutions where B/C and G*H/I are integers: Code: \<< Code: { { 5 4 1 9 2 7 3 8 6 } Cheers, Werner 41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE |
|||
05-24-2015, 08:20 PM
Post: #30
|
|||
|
|||
RE: Vietnamese snake puzzle - Closed
(05-23-2015 09:08 AM)Werner Wrote: I've written an RPL solution as well that, on EMU48 at full pelt, cranks out all 136 solutions in 30 seconds. But I'm not happy with it yet. I've decided to change my RPL program to handle c for other values than 1, since it was just a matter of including another loop and do the necessary modifications. Perhaps the most difficult one would be determining the new the MOD condition, but I shamelessly used yours above (Thanks! :-). I've obtained 34 solutions (136/4) as expected, in 49.47 seconds on the emulator (Debug4x, Emu-50g, WinXP, Intel Core Duo CPU @ 1.86 GHz) -- 23 minutes and 31.9 seconds on the physical HP 50g. Ok, each solution should be processed to generate three additional ones, but that would take irrelevant time so I won't mind doing it. Cheers, Gerson. Code:
{ { 7. 6. 4. 8. 5. 9. 1. 3. 2. } { 9. 6. 4. 3. 5. 8. 1. 7. 2. } { 1. 9. 6. 4. 5. 8. 3. 7. 2. } { 9. 4. 8. 5. 6. 7. 1. 3. 2. } { 2. 8. 6. 9. 4. 1. 5. 7. 3. } { 2. 6. 9. 8. 5. 1. 4. 7. 3. } { 6. 3. 1. 9. 2. 5. 7. 8. 4. } { 7. 3. 1. 5. 2. 6. 8. 9. 4. } { 7. 3. 2. 8. 5. 9. 1. 6. 4. } { 5. 7. 2. 8. 3. 9. 1. 6. 4. } { 8. 9. 2. 3. 1. 5. 6. 7. 4. } { 5. 9. 3. 6. 2. 1. 7. 8. 4. } { 3. 2. 8. 6. 5. 1. 7. 9. 4. } { 7. 2. 8. 9. 6. 5. 1. 3. 4. } { 3. 2. 1. 5. 4. 7. 8. 9. 6. } { 9. 4. 1. 5. 2. 7. 3. 8. 6. } { 1. 3. 2. 4. 5. 8. 7. 9. 6. } { 7. 5. 2. 8. 4. 9. 1. 3. 6. } { 8. 5. 2. 1. 4. 7. 3. 9. 6. } { 1. 5. 2. 3. 4. 8. 7. 9. 6. } { 9. 5. 3. 1. 4. 2. 7. 8. 6. } { 3. 2. 4. 8. 5. 1. 7. 9. 6. } { 1. 4. 8. 2. 7. 9. 3. 5. 6. } { 1. 3. 9. 4. 7. 8. 2. 5. 6. } { 9. 1. 2. 5. 6. 7. 3. 4. 8. } { 9. 3. 2. 1. 5. 6. 4. 7. 8. } { 7. 1. 4. 9. 6. 5. 2. 3. 8. } { 2. 1. 4. 3. 7. 9. 5. 6. 8. } { 7. 3. 4. 1. 6. 5. 2. 9. 8. } { 1. 3. 6. 2. 7. 9. 4. 5. 8. } { 7. 9. 6. 1. 5. 2. 3. 4. 8. } { 2. 9. 6. 3. 5. 1. 4. 7. 8. } { 7. 8. 3. 1. 4. 5. 2. 6. 9. } { 1. 2. 6. 4. 7. 8. 3. 5. 9. } } I think this is correct, but I haven't checked it yet. |
|||
05-24-2015, 08:34 PM
(This post was last modified: 05-24-2015 08:35 PM by Gerson W. Barbosa.)
Post: #31
|
|||
|
|||
RE: Vietnamese snake puzzle - Closed
(05-24-2015 06:40 PM)Werner Wrote: My RPL solution to the arithmetic puzzle (long): This means about 20 minutes on a real HP-48G. Mine takes 23m 32s on the HP 50g, but the 50g is about 5 to 7 times faster than the 48G. Also, high quality documentation -- unlike mine (none). Congratulations! Gerson. |
|||
05-25-2015, 06:33 AM
Post: #32
|
|||
|
|||
RE: Vietnamese snake puzzle - Closed
(05-24-2015 08:20 PM)Gerson W. Barbosa Wrote: I think this is correct, but I haven't checked it yet. It passes the test: Code: \<< Though none of your solutions matches mine ;-) You take a different A/D order. Cheers, Werner 41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE |
|||
05-25-2015, 07:14 AM
Post: #33
|
|||
|
|||
RE: Vietnamese snake puzzle - Closed
What a can of snakes I opened.
& then I innocently believed it was done when all solutions were found! |
|||
05-26-2015, 04:54 AM
(This post was last modified: 05-26-2015 09:54 PM by Gerson W. Barbosa.)
Post: #34
|
|||
|
|||
RE: Vietnamese snake puzzle - Closed
(05-25-2015 06:33 AM)Werner Wrote:(05-24-2015 08:20 PM)Gerson W. Barbosa Wrote: I think this is correct, but I haven't checked it yet. I tried to optimize the innermost loop so that one, two or three permutations of the candidates to a, d and f were made as needed, rather than always three, but the DO-UNTIL loop ended up being slower than START-NEXT. Now it takes almost one minute longer: 24 min 53 sec. The output list pass the test also, mainly because it's the same list, only with the elements in reverse order :-). The two nested FOR-NEXT loops one level higher generate the 10 combinations [ C(5,2)] of the five candidates to g, h, a, d and f. An optimization here might help. The HP 50g has C2P, P2C and CIRC, but I don't know if those can be used for this purpose. Cheers, Gerson. Code:
P.S.: Time on the HP 49G: 1h 09m 32s. 1h 12m 25s on the HP 48GX. HP 48GX version: Code:
|
|||
05-26-2015, 08:57 PM
Post: #35
|
|||
|
|||
RE: Vietnamese snake puzzle - Closed
(05-24-2015 06:40 PM)Werner Wrote: My RPL solution to the arithmetic puzzle (long): Thanks a lot Werner for your usefull and efficiant DOPERM program ! Very interesting. It's now in my favorite programs ;D I wonder what could be the gain of a sys-RPL version ? Strangely there is no (as far I know) permutations library for the 50G. |
|||
05-27-2015, 10:12 AM
Post: #36
|
|||
|
|||
RE: Vietnamese snake puzzle - Closed
@Gilles: thanks. I have a 'DOCOMB' lying around for years as well, that will let you execute an object for all combinations of m (input) out of n (size of list) objects.
For the arithmetic puzzle: The G-H equivalence can be implemented in a much easier way: just test G>H (or vice versa) at the appropriate level ( 5 here): Code: { 1 2 3 4 5 6 7 8 9 } And it's even slightly faster. Cheers, Werner 41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE |
|||
05-28-2015, 09:20 PM
Post: #37
|
|||
|
|||
RE: Vietnamese snake puzzle - Closed
(05-23-2015 09:08 AM)Werner Wrote: Including the condition that I've changed to the condition FP(13*b/c + g*h/i)=0, which is simpler and uses four multiplication/division operations instead of six. Since I take 13*b/c from the stack, this is reduced to only one multiplication and one division in the inner loop. Also, local variables 'a', 'd' and 'f' are not defined anymore. Running times are now 1h 05m 32.9s on the HP-48GX and 21m 51.9s on the HP 50g. I should revert to my original innermost loop, which apparently is faster, but I prefer to optimize the current one later. I'd be pleased with less than one hour's time on the HP-48GX :-) Cheers, Gerson. HP-48GX: Code:
HP 50g: Code:
|
|||
05-29-2015, 07:14 AM
Post: #38
|
|||
|
|||
RE: Vietnamese snake puzzle - Closed
Quote:I've changed to the condition FP(13*b/c + g*h/i)=0 Yes, I changed it to avoid rounding errors, but it turns out in the 48's decimal arithmetic we don't have any - in hindsight ;-) My latest posted routine to find the 5 'full integer' solutions runs in 375 seconds on the EMU48 at 'authentic speed' setting, which should not be far off. To find all solutions it needs approx. 1212 seconds or 20 minutes. Cheers, Werner 41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE |
|||
05-31-2015, 05:37 AM
Post: #39
|
|||
|
|||
RE: Vietnamese snake puzzle - Closed
(05-29-2015 07:14 AM)Werner Wrote:Quote:I've changed to the condition FP(13*b/c + g*h/i)=0 In addition to the condition e < 8, now I am using -17 < (f - a - d) < 7, which I had done when manually searching for a solution. Now the primary 34 solutions are found in 52m 15.9s on the HP-48GX and in 17m 38.7s on the HP 50g (17m 40.4s in the HP 50g version using DO-UNTIL instead of START-NEXT - definitely not worth in this case since most f, a, d in agreement with the second condition will have to be permutated three times anyway). I think finding all solutions under one hour's time on the HP-48GX is quite acceptable :-) Cheers, Gerson. HP-48GX: Code:
HP 50g: Code:
Code:
|
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 12 Guest(s)