Happy New Year 2024 ... and 2023's last teaser ! - Printable Version +- HP Forums (https://www.hpmuseum.org/forum) +-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html) +--- Forum: General Forum (/forum-4.html) +--- Thread: Happy New Year 2024 ... and 2023's last teaser ! (/thread-21078.html) Pages: 1 2 |
RE: Happy New Year 2024 ... and 2023's last teaser ! - 3298 - 01-03-2024 12:08 PM (01-03-2024 12:56 AM)Valentin Albillo Wrote:It is when I'm afraid of spoiling the challenge for others. For instance, in the (online version of the) HHC contests I generally submit program size and checksum only (and maybe a cryptic hint), withholding the code and proper explanation until after the deadline is over.3298 Wrote:Solved it without even bothering to fetch and unpack a calc. Hint: Boolean logic and tracing diagonals through the matrix with a finger was enough. Happy new year anyway! However, this time I misunderstood the challenge, so i "solved" something that was far easier than the actual challenge, enabling me to do so without a calculator. In my defense, I never needed to calculate the determinant of a matrix larger than 3x3 by hand, so I failed to realize that the rule of thumb I remembered from my school days will not work beyond this size. Based on that I ended up with a 0 result, which surprised me only in the way that your previous challenges usually resulted in the number naming the old or the new year. It seemed like the obvious answer otherwise ... again, due to my flawed understanding. It kind of felt too easy for one of your challenges, almost boring actually. (That's what prompted the "anyway" part of my post...) It should have made me realize that I got something wrong. I feel stupid now. On a slightly more serious note, the RPL code looks clearer to me than your Basic code. The names of the RPL commands called throughout are a better documentation than trying to trace what each Basic variable is subjected to. But I'm RPL-trained, which allows me to see stackrobatics as necessary but uninteresting fluff, much like variable assignments in a C-derived language. Readability is subjective. RE: Happy New Year 2024 ... and 2023's last teaser ! - Gerson W. Barbosa - 01-03-2024 12:17 PM (01-03-2024 12:56 AM)Valentin Albillo Wrote:Gerson W. Barbosa Wrote:Happy New Year You All! This should work only for 2x2 and 3x3 matrices, so not quite relevant for that particular problem [...] Hi, Valentín, There’s a mistake in my program. I’ve used RANK when all I need with the order of the matrix. As a result it was taking longer that it needed to for a 3x3 matrix (0.81 seconds instead of 0.23 seconds). This is better: Code:
Not any more readable than the previous version though. It’s based on a little formula I came up with. Thanks for this teaser, even if I wasn’t up to the task. Until then I didn’t know there was something like the permanent of a matrix. Best regards, Gerson. ——————— Per(M) = Det(M’) - 2p where M’ is M with elements of the main diagonal multiplied by -1 and p is the product of the elements of the main diagonal of M’. This works for order 3 matrices. For order 2 matrices the sign of the result must be changed. For example, M: [[ -1 3 2 ] [ 7 -4 5 ] [ 6 8 9 ]] M’: [[ 1 3 2 ] [ 7 4 5 ] [ 6 8 -9 ]] Det(M’) = 267 p = 1×4×(-9) = -36 Per(M) = 267 - 2×(-36) = 339 RE: Happy New Year 2024 ... and 2023's last teaser ! - Ajaja - 01-03-2024 12:51 PM There are many rows and columns with zeros. I think, the fastest way to calculate permanent of the matrix is to sort it first, then transpose, sort again and after that use J-F Garnier optimized subprogram. It turns this 12x12 permanent into sum of 8 9x9 permanents if I'm not mistaken. And result should be the same: Code:
RE: Happy New Year 2024 ... and 2023's last teaser ! - John Keith - 01-03-2024 02:59 PM (01-03-2024 12:17 PM)Gerson W. Barbosa Wrote: Per(M) = Det(M’) - 2p That is quite interesting. In my (obsolete) program, I compute the 3 x 3 permanent with inline code as follows, requiring 9 multiplications and 5 additions. Code:
This may be faster than using DET. RE: Happy New Year 2024 ... and 2023's last teaser ! - Gerson W. Barbosa - 01-03-2024 05:02 PM (01-03-2024 02:59 PM)John Keith Wrote:(01-03-2024 12:17 PM)Gerson W. Barbosa Wrote: Per(M) = Det(M’) - 2p You’re definitely right! Actually about 4.5 times as fast. Size is the only advantage here (67 bytes versus 100 bytes). Code:
RE: Happy New Year 2024 ... and 2023's last teaser ! - ramon_ea1gth - 01-03-2024 06:35 PM (01-03-2024 12:56 AM)Valentin Albillo Wrote: ¡ Hola, paisano, Feliz Año Nuevo 2024 !¡Hola Valentín! I have to refrain tackling your challenges. They are addictive! Even after finishing I was back revising my, anyway, suboptimal code, realising that I can save more operations with binary matrix values. It was a good excuse to think and read further from your hints: now I also find challenging how to prepare the matrix with the predefined result. ¡Feliz Año Nuevo también! RE: Happy New Year 2024 ... and 2023's last teaser ! - J-F Garnier - 01-04-2024 01:01 PM (01-03-2024 12:51 PM)Ajaja Wrote: There are many rows and columns with zeros. I think, the fastest way to calculate permanent of the matrix is to sort it first, then transpose, sort again and after that use J-F Garnier optimized subprogram. If you look closely at my solution, you will see that I sorted the array rows by the number of 1 in each row, not by the value of the binary representation: 20 DATA 0,0,1,0,0,0,0,0,0,1,0,0 21 DATA 0,0,1,0,0,0,0,0,0,1,0,1 22 DATA 0,0,1,1,0,0,0,0,0,1,0,1 23 DATA 0,0,1,1,0,0,0,0,0,1,1,1 24 DATA 1,0,1,1,0,0,0,0,0,1,1,1 25 DATA 1,0,1,1,0,0,0,1,0,1,1,1 26 DATA 1,0,1,1,1,0,0,1,0,1,1,1 27 DATA 0,1,0,1,1,1,1,1,1,1,0,1 28 DATA 1,0,1,1,1,0,1,1,0,1,1,1 29 DATA 1,0,1,1,1,0,1,1,1,1,1,1 30 DATA 1,0,1,1,1,1,1,1,1,1,1,1 31 DATA 1,1,1,1,1,1,1,1,1,1,1,1 This was the most important part of my 'optimization'. The other one was to consequently skip the minor calculation for the zero elements: 135 IF A(1,K)=0 THEN 145 ! little optimization In that way, the recursive tree was reduced to 2 paths at each level instead of 11 (up to row 8), mostly important for the first recursive steps to cut calculation time. I did notice the structure of the sorted array and the exception of row 8 (of the sorted array), but without paying too much attention to it. I was quite happy to get a solution in HP BASIC in a sensible time, even on a machine as slow as the 71B. Of course, as Valentin pointed out, this solution is only effective for this case with this special array structure. J-F RE: Happy New Year 2024 ... and 2023's last teaser ! - Gjermund Skailand - 01-05-2024 10:11 AM Update/Small speed improvement Here is a sys-RPL implementation of Valentin's code for a general matrix computation of permanent : Compiles to 255.5 bytes, chksum #917Dh On my HP50g it solves the 12x12 matrix in slightly less than 13,5 minutes. calculations can be aborted by pushing ON/cancel Code:
best regards Gjermund RE: Happy New Year 2024 ... and 2023's last teaser ! - Gjermund Skailand - 01-05-2024 10:12 AM How do I avoid that space indenting is not stripped away??) RE: Happy New Year 2024 ... and 2023's last teaser ! - Didier Lachieze - 01-05-2024 10:48 AM (01-05-2024 10:12 AM)Gjermund Skailand Wrote: How do I avoid that space indenting is not stripped away??) By using the code tags (the # in the message editor toolbar). RE: Happy New Year 2024 ... and 2023's last teaser ! - Gjermund Skailand - 01-05-2024 10:56 AM Thanks, Didier I was curious to test the real speed of hp50g, running hpgcc3 code at 120MHz, so I ported to a hpgcc3 program, 1084bytes, program and code, enclosed. This program calculates the 12x12 matrix in less than 1 sec. It calculates permanents up size {15,15} in about 4 sec. It requires a modified rom to run this, but if anyone is interested, let me know. PS anyone got timings on other calculators? best regards Gjermund |