When writing code-golf programs, I usually end up using some numeric constants. I always put them in decimal because that's how I think, but I just realized that my language supports other number formats that might let me shorten my code slightly.
Challenge
Given a non-negative integer less than 2^53-1, decide whether that integer has the shortest representation in:
- Decimal
- Hexadecimal
- Scientific Notation
Decimal
Since this is the default format of my language, there is no extra notation needed for this format. Every number is represented as usual for decimal.
Hexadecimal
My languages uses the 0x
prefix for hexadecimal constants. This means that if a number has 4 hexadecimal digits, it will take 6 bytes to represent that number.
Scientific notation
My language uses the following format for scientific notation:
[Real base]e[Integer exponent of 10]
For example, 700
would be represented as 7e3
, and 699
would be represented as 6.99e3
, because the base must be between -10 and 10 (non-inclusive). For the purposes of this challenge, the base will always be at least 0, since the inputted number is non-negative.
Output
You should return a way of identifying which format is shortest (i.e. 0 for decimal, 1 for hex, 2 for scientific). Alternatively, you may output the smallest representation of the number itself.
Test cases
Decimal | Hexadecimal | Scientific | Winner
--------------|--------------|-------------------|-------------
0 | 0x0 | 0e0 | Decimal
15 | 0xF | 1.5e1 | Decimal
6999 | 0x1B57 | 6.999e3 | Decimal
7000 | 0x1B58 | 7e3 | Scientific
1000000000000 | 0xE8D4A51000 | 1e12 | Scientific
1000000000001 | 0xE8D4A51001 | 1.000000000001e12 | Hexadecimal
1000000001000 | 0xE8D4A513E8 | 1.000000001e12 | Hexadecimal
1000001000000 | 0xE8D4B45240 | 1.000001e12 | Scientific
Scoring
This is code-golf, so the answer in the shortest bytes for each language wins.
2^63-1
may be difficult for some languages. Consider relaxing that to a lower value such as2^32-1
(so the values fit in a double floating point data type) \$\endgroup\$double
. Just a suggestion; do as you see fit \$\endgroup\$1000001000000
can also be written as1000001e6
though. \$\endgroup\$