Your goal is to write some code that will output the shortest unique decimal sequence for the input fraction. No two fractions with the same denominator may have the same output, although it's possible for fractions with different denominators to have the same representation.
Take 2 integers as input, the first is the numerator, the second is the denominator.
E.g.:
n d output
----- ------
0 13: 0.00
1 13: 0.07
2 13: 0.1
3 13: 0.2
4 13: 0.30
5 13: 0.38
etc.
3/13
is the only fraction with a denominator of 13 that starts with 0.2
, so no further digits are required. 4/13
and 5/13
both start with 0.3
, so another digit is required to distinguish between them.
You may output numbers greater than -1 and less than 1 either with or without a zero before the decimal point, as long as the output is consistent, i.e. 0.5
and .5
are the same number and are both valid. No other leading zeros are allowed. Trailing zeros must be shown if they are necessary to distinguish the output from another value.
You may not round any numbers away from zero; they must be truncated. There must be no leading or trailing spaces. There may optionally be a single trailing newline.
More test values:
n d output
---------- ------
0 1: 0 (this 0 may not be removed because there's no decimal point)
5 1: 5
0 3: 0.0 (or .0)
4 3: 1.3
5 3: 1.6
10 8: 1.2
11 8: 1.3
12 8: 1.5
-496 -38: 13.05
458 -73: -6.27
70 106: 0.660 (or .660)
255 123: 2.07
256 -123: -2.081
-257 -123: 2.089
-258 123: -2.09
258 -152: -1.697
-259 152: -1.70
260 152: 1.710
272 195: 1.39
380 247: 1.538
455 -455: -1.000
-44 891: -0.049 (or -.049)
123 1234: 0.099 (or .099)
In each case, the output and the denominator are sufficient to uniquely work out the numerator.