The other day my chemistry teacher was explaining to us about scientific notation (using a small number and multiplying it by powers of ten to express large numbers more easily), which brought me back a few years to when I first learnt it. After learning the basics, we had done a bunch of typical maths questions, some of which were like the following:
Represent the following in scientific notation:
And I thought, "What? We were told that scientific notation was used to make writing large numbers more efficient, but some cases aren't more efficient at all!"
Consider the number
and its representation in scientific notation:
What, the scientifically notated version actually takes up more space? We can't have that now can we? (Screen space is precious.)
We could determine ourselves if it's more space efficient to write a number in scientific notation or not, or...
Your program or function should take as input a single positive number
n of arbitrary size (up to what your language supports) and output the scientifically notated version of the number.
However, if the original number
n, after removal of trailing zeroes and trailing decimal place, takes less or the same amount of characters to display than its scientifically notated version, you must output that original number
Your code needs to be as short as possible because the output also has to be as short as possible.
Efficient Scientific Notation is defined as follows:
b is the input number appropriately divided by powers of 10 such that
1 <= b < 10. This number must have all trailing zeroes (and decimal point if required) removed, but must have the precision of the original number (up to the decimal point limit in your language, of course). Ie
6.75 etc. If this number ends up containing more decimal places than your language can handle, it should be rounded to that maximum number of decimal places.
e is the exponent to which ten is raised such that
n = b x 10^e (remember that this number needs to be negative if
n is smaller than 1). This number should not have any trailing zeros or a decimal place (mainly because if it's not an integer something is wrong...).
x10^ must remain as is in the string between
Input -> output 1 -> 1 20 -> 20 3000000 -> 3x10^6 400000 -> 400000 0.008093 -> 0.008093 0.007835000000000 -> 0.007835 0.000003000000 -> 3x10^-6 0.00000065 -> 6.5x10^-7 0 -> 0
This is code-golf, so shortest code in bytes wins.
Other rules and clarification
- Trailing zeros (and/or trailing decimal place) are not counted towards the character count of the original input number
n. Keep that in mind for cases such as test case 6
- You may assume that if the input number is less than 1, it will always start with a 0 in place for the ones digit (as in test cases 5-8).
- Input number will never be negative
- Built-ins that make this challenge trivial and standard loopholes are disallowed
- A trailing newline in the output is OK
Thanks to user81655 for pointing out test cases 7 and 8 had incorrect powers of ten. I have now fixed those so make sure your code evaluates them correctly.