What general tips do you have for golfing in MathGolf? I'm looking for ideas that can be applied to code golf problems in general that are at least somewhat specific to MathGolf (for tips which can be applied to loads of programming languages, look at the general tips).
2 Answers
Writing 513
in a MathGolf program will push three separated digits (5
;1
;3
), instead of the number 513
itself.
In MathGolf there are therefore quite a lot of 1-byte builtins for numbers: -3
through and including 38
; 60
; 64
; 100
; 128
; 256
; 512
; 1000
; 1024
; 2048
; 3600
; 4096
; 10000
; 86400
; 100000
; 1000000
; 10000000
; 100000000
.
However, for other numbers it can sometimes be tricky to find the shortest way to generate it, so here an overview of the integers in the range [1, 1000]
:
NOTE: This is W.I.P. I will add more when I have some time. I was unable to make an evaluation program to check all possibilities (thus far), so these were done by hand.. I've also made it a community wiki, so if anyone else finds shorter alternatives or wants to continue with the numbers, feel free to edit it.
Of course many variations can be found for certain numbers, as can be seen here and here. So the list below contains just one of the shortest variations.
-3-0: d, c, b, 0
1-10: 1, 2, 3, 4, 5, 6, 7, 8, 9, ♂
11-20: A, B, C, D, E, ☻, F, G, H, I
22-30: J, K, L, M, N, O, P, Q, R, S
31-40: T, ♥, U, V, W, X, Y, Z, Z), Z⌠
41-50: Dx, J∞, Vx, K∞, K∞), L∞, L∞), M∞, 7², N∞ TODO: 2-byters for 45,47
51-60: Ex, O∞, Wx, P∞, ♂f, Q∞, ╟╫, R∞, ╟(, ╟
61-70: ☻x, T∞, Xx, ♦, ♦), U∞, U∞), V∞, V∞), W∞ TODO: 2-byters for 67,69
71-80: Fx, X∞, Yx, Y∞, Y∞), Z∞, Z∞), Z∞⌠, ♦E+, ♦F+ TODO: 2-byters for 75,77,78,79,80
81-90: Gx, Qx, Zx, Zx), Zx⌠, ♦K+, ♦L+, R■, ♂f, ♦O+ TODO: 2-byters for 84,85,86,87,90
91-100: Hx, Rx, ♦R+, T■, ♦T+, ♦♥+, ♦U+, ♀⌡, ♀(, ♀ TODO: 2-byters for 93,95,96,97
TODO: Continue through 1000
Here is a universal constant-generating method in MathGolf. Example (generating 513):
û☺☻$
This defines a two-character string and takes its ord code. The ord code of ☻
is 2, and the ord code of ☺
is 1. The ord code of this string is therefore 2*256+1=513
.
Taking the ord code also works for single characters. (Constants generated by this method are likely to be unoptimal.) This one returns 2.
'☻$
Also works for string length 3 and so on...
ùUTS$
83*256*256+84*256+85=5461077
-
\$\begingroup\$ Can this be brute forced (with a time-out)? \$\endgroup\$ Commented Oct 9, 2019 at 9:20
-
\$\begingroup\$ @someone "I was unable to make an evaluation program to check all possibilities (thus far), so these were done by hand.." ;) I'm pretty sure it should be possible with a pure bash script instead of inside the MathGolf TIO, because MathGolf will stop with an error quite easily. Getting all possible pairs and just using eval in MathGolf itself already errors on the very first combination. The language is still pretty unstable imho. \$\endgroup\$ Commented Oct 9, 2019 at 9:22
-
-
\$\begingroup\$ @Grimy You could edit it in yourself, I've made it a comm wiki anyway. And I'm an idiot for missing
7²
. As for the other two, I didn't even knew MathGolf had a Fibonacci builtin. :) \$\endgroup\$ Commented Oct 9, 2019 at 12:15 -
\$\begingroup\$ @someone For a list it will do the cartesian product with itself: Try it online. For an integer, it will output the n'th Collatz number (which is basically
3n+1
for odd numbers; andn/2
for even numbers - although this second part is less useful because there is already a builtin for that): Try it online. \$\endgroup\$ Commented Oct 9, 2019 at 16:18
This post tries to explain how the compressed strings work in MathGolf (a canonical feature for golfing languages).
This mechanism is actually quite simple: say you are trying to decompress the character Σ
via the ╢
instruction.
First of all, this character is 0xE4 in CP437. We then pass this through a lookup table. (We are decompressing using ╢
, which has the lookup table etaoinsrdluczbfp
. There is one other such table gwymvkxjqh ?*#.,
.)
We realize that the E'th character (in hex) in the lookup table is f. The 4th character in the lookup table is i
. So we get the decompressed string fi
.
The way I do it is that I check the reference page and Code page 437, and I match the column and the row of the character within the code page with the indices of the string I want. So, ╕ means indexing in the string etaoinsrdluczbfp. In that string, b has index 13, z has index 12, and t has index 1. Thus, I look at row 13 and column 12 for bz, which is character ▄. Then I look at row 12 and column 1 for zt, which is character ┴. --maxb
For the sake of completeness I will also explain the dictionary: ╩
pushes top 256 dictionary words. The word is indexed by the next character in the program, and the index is 0-indexed. ╦
uses a number on the stack.
-
\$\begingroup\$ I see you've already figured it out. I was about to post this comment: "Well, the string compression part is on the TODO list to be changed, since right now it isn't possible to mix characters of
etaoinsrdluczbfp
andgwymvkxjqh ?*#.,
". But when I asked maxb about the compression, he briefly explained it in the comments here. \$\endgroup\$ Commented Oct 10, 2019 at 14:36 -
\$\begingroup\$ Maybe also useful to mention that it isn't strictly CP437. EDIT: Although I'm not sure the null-byte is still part of the encoding. I know Max changes a command from a null-byte to something else because it was hard to type, but not sure what the null-byte represents right now. \$\endgroup\$ Commented Oct 10, 2019 at 14:39