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).
513 in a MathGolf program will push three separated digits (
3), instead of the number
In MathGolf there are therefore quite a lot of 1-byte builtins for numbers:
-3 through and including
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
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
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...
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
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
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
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.