25
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.
Relevant – musicman523 – 2017-06-02T14:12:39.360
1The requirement to go up to
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) – Luis Mendo – 2017-06-02T14:14:33.133To clarify: we can either output all 3 reprsentations of the number or the name of the single representation that uses the least amount of characters? – Shaggy – 2017-06-02T14:15:07.503
@LuisMendo Unfortunately hex never wins below 2^32, which is why I made this requirement – musicman523 – 2017-06-02T14:15:46.477
1I see. How about 2^52-1? That still fits in
double
. Just a suggestion; do as you see fit – Luis Mendo – 2017-06-02T14:16:51.330@Shaggy you should either:
@LuisMendo Updated. My original intent was for C/C++/Java users to use
long
(and Scala to useLong
), but if it's easier to use doubles then go for it – musicman523 – 2017-06-02T14:21:57.543Gotcha! Thanks for clearing that up. Can we take input as a string? – Shaggy – 2017-06-02T14:22:23.823
I think the limit for
double
is2^53-1
, so you could still increase by a factor of2
– Luis Mendo – 2017-06-02T14:29:15.410@Shaggy I'm not sure, I can't find a consensus on that issue. If you can find one in the codegolf meta, could you link it here? Otherwise for now, just leave a footnote on your answer showing how it can be shortened with the alternative input format – musicman523 – 2017-06-02T14:29:59.423
@LuisMendo Thanks, updated – musicman523 – 2017-06-02T14:30:05.987
1
1000001000000
can also be written as1000001e6
though. – Erik the Outgolfer – 2017-06-02T14:33:48.613@EriktheOutgolfer AFAIK most languages don't use scientific notation like that – Stephen – 2017-06-02T14:38:36.690
@EriktheOutgolfer Sorry, I should have specified more clearly - this challenge uses the standard form for scientific notation
– musicman523 – 2017-06-02T14:41:34.750@StephenS Output from
repr
-like functions won't be like that but you can perfectly write that as a literal. – Erik the Outgolfer – 2017-06-02T14:45:07.070@musicman523 ooh that's actually quite challenging – Erik the Outgolfer – 2017-06-02T14:45:22.533
If two notations tie (e.g. hex & dec for
10000000001
) may we output just one? May we output both? – Jonathan Allan – 2017-06-02T14:45:59.183You may do either – musicman523 – 2017-06-02T14:46:24.617
...and may we output a list of identifiers in order? – Jonathan Allan – 2017-06-02T14:48:25.790
1@JonathanAllan yes, that was @ you, sorry. And no, you may not output the ordered list; since this is a [tag:decision-problem], you need to decide on one single output. (But your implementation may sort the list and output the first item.) – musicman523 – 2017-06-02T14:53:29.503
Annoyingly in C#
ulong.ToString("e")
, converting to scientific notation, loses precision on the larger numbers. So I'm going to have to do that conversion manually. – TheLethalCoder – 2017-06-02T15:11:16.1831Isn't a [tag:decision-problem] by definition only supposed to have two possible outputs? – mbomb007 – 2017-06-02T15:52:04.107
It looks like, according to this meta post, it should be a [tag:classification] problem. I'll make this change.
– musicman523 – 2017-06-02T17:35:33.540I'm sandboxing Part 2 now, if anyone would like to help!
– musicman523 – 2017-06-02T20:29:32.727