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:
a) 50000000
b) 120000000000000
c) 90000000000000000000000000000000000000
d) pi^e^i^j^k^std::vector
...
z) 200
...
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
300
and its representation in scientific notation:
3x10^2
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...
Task
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 n
instead.
Your code needs to be as short as possible because the output also has to be as short as possible.
Specifications
Efficient Scientific Notation is defined as follows:
bx10^e
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 90000
becomes 9
, 13.500
becomes 1.35
, 0.000675
becomes 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...).
The characters x10^
must remain as is in the string between b
and e
.
Test cases
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
Scoring
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
EDIT
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.
pi^e^i^j^k^std::vector
would be? \$\endgroup\$e
:9000 -> 9e3
(almost over 9,000!) \$\endgroup\$x10^
. And it would be a fair bit of rework on the question, which I don't think is that appropriate now that it's posted \$\endgroup\$