15
\$\begingroup\$

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.

\$\endgroup\$

3 Answers 3

1
\$\begingroup\$

Perl, 77 bytes

#!perl -p $%++while/ /<grep{!index$_/$',$\=$`/$'.($`%$'?0:n).0 x$%&'?'x$%}$`-2..$`+2}{ 

Counting the shebang as one, input is taken from stdin.

Sample Usage

$ echo 0 3 | perl golf-decimals.pl 0.0 $ echo 4 3 | perl golf-decimals.pl 1.3 $ echo 11 8 | perl golf-decimals.pl 1.3 $ echo -496 -38 | perl golf-decimals.pl 13.05 $ echo 458 -73 | perl golf-decimals.pl -6.27 $ echo -44 891 | perl golf-decimals.pl -0.049 
\$\endgroup\$
1
\$\begingroup\$

Pyth, 37 bytes

AQJ+`cGHK*20\0<Jf!}<JTm<+`dKTcRH,tGhG 

A program that takes input in the form numerator,denominator and prints the result.

Test suite

[Explanation coming later]

\$\endgroup\$
1
\$\begingroup\$

JavaScript (ES7), 118 93 90 bytes

f=(a,b,i=0)=>(v=(p=n=>((n/b*10**i|0)/10**i).toFixed(i))(a))==p(a+1)|v==p(a-1)?f(a,b,i+1):v 

I saved 25 bytes, thanks to @Neil.
Saved additional 3 bytes by using recursion.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ You always pass /b and i to p so you might as well code them inside p itself and just take a single parameter. Also the answer is just n so you don't have to calculate it again. I have a recursive ES6 version loosely based on this at a mere 86 bytes... \$\endgroup\$ Commented Dec 10, 2016 at 22:41

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.