Shortening a long number

10

0

When dealing with long numbers when code golfing, getting the length of your code down can be difficult, so do you have any tips for making a number shorter and easier to manage?

For example, the following Pyth code prints "Hello world", but at 44 bytes, that is unsatisfactory:

Vcjkj85942000775441864767076 2 7=k+kCiN2)k

So how would I shorten the long number, 85942000775441864767076?

Beta Decay

Posted 2015-08-16T12:42:43.793

Reputation: 21 478

Answers

10

Perfect timing for this question. @isaacg just added a new feature today, that allows to shorten such numbers immensely.

The basic technique is to convert the number to base 256 and convert it to chars. You can do this using the code ++NsCMjQ256N. You can then use the resulting string in combination with C, which does exactly the opposite (convert chars to int and interpret the result as base-256 number). So you get 13 chars: C"2ìÙ½}ü¶d". Some of the chars are unprintable.

But notice that I said 13 CHARS, not bytes. If I copy the chars and count the with https://mothereff.in/byte-counter, it says 13 chars and 18 bytes. This is due the character encoding of chars, which is UTF-8 by default. And UTF-8 only allows 2^7 different 1-byte character. Every char c with ord(c) > 127 actually gets stored using two bytes instead of one.

And here come's @isaacg's new feature into play. He changed the default code-format from UTF-8 to iso-8859-1. iso-8859 can represent 256 characters with only 1 byte. So now you can actually reach 13 BYTES. This is only possible with the standard compiler though, this doesn't work in the online compiler.

First you want to convert the number to hex-values by using this script: jdm.[2.Hd"0"jQ256. This gives you 12 32 ec d9 bd 07 7d fc b6 64. Afterwards copy those numbers into your code-file using a hex-editor (e.g. hexedit for linux).

hexedit demonstration

Notice:

  • Obviously you remove the " at the end, if the string is the last part of the code.
  • This only works if there is no 34 (the byte 22) in the base-256 representation of you numbers, since this is the " char and will end the string. Escaping works though (5C 22).
  • Btw, when you open a file with a hex-editor you will likely see the byte 0A or 0d 0a at the end, which you can remove. This only indicates the end of the line.

Jakube

Posted 2015-08-16T12:42:43.793

Reputation: 21 462

I'll have to put the offline interpreter on my machine, but this is brilliant, thanks! :) – Beta Decay – 2015-08-16T16:12:10.657

1The string also can't include the null byte, of value 00. In addition, \, byte 5c, may or may not need to be escaped with another \, depending on the byte after it. – isaacg – 2015-08-17T08:39:47.510