24
7
Task:
Your program is given a proper, positive simple fraction in the format <numerator>/<denominator>
.
For this input, it must find two fractions.
- A fraction that is less than the input.
- A fraction that is greater than the input.
Both fractions must have a lower denominator than the input. Of all possible fractions, they should have the lowest difference to the input.
Output:
Your program's output must be:
- A fraction that is smaller than the input, in the format
<numerator>/<denominator>
. - Followed by a space character (ASCII-code 32).
- Followed by a fraction that is greater than the input, in the format
<numerator>/<denominator>
.
As follows:
«fraction that is < input» «fraction that is > input»
Rules:
- All fractions outputted must be in lowest terms.
- All fractions outputted must be proper fractions.
- If there are no proper fractions possible that are allowed by the rules, you must output
0
instead of a fraction < input, and1
instead of a fraction > input. - You can choose whether you want to receive the fraction as a command-line argument (e.g.
yourprogram.exe 2/5
) or prompt for user input. - You may assume your program won't receive invalid input.
- The shortest code (in bytes, in any language) wins.
Any non-standard command-line arguments (arguments that aren't normally required to run a script) count towards the total character count.
What your program must not do:
- Depend on any external resources.
- Depend on having a specific file name.
- Output anything other than the required output.
- Take exceptionally long to run. If your program runs over a minute for fractions with a 6-digit numerator and denominator (e.g.
179565/987657
) on an average home user's computer, it's invalid. - Output fractions with
0
as the denominator. You can't divide by zero. - Output fractions with
0
as the numerator. Your program must output0
instead of a fraction. - Reduce an inputted fraction. If the fraction given as input is reducible, you must use the fraction as it is inputted.
- Your program must not be written in a programming language for which there did not exist a publicly available compiler / interpreter before this challenge was posted.
Examples:
Input: 2/5
Output: 1/3 1/2
Input: 1/2
Output: 0 1
Input: 5/9
Output: 1/2 4/7
Input: 1/3
Output: 0 1/2
Input: 2/4
Output: 1/3 2/3
Input: 179565/987657
Output: 170496/937775 128779/708320
2Your last example is incorrect. The input fraction equals the first of the output fractions. – DavidC – 2015-04-15T21:24:33.477
@David Oops, you're right. Fixed now (I hope). (Just in case I got it wrong again: I wrote this piece of code to calculate the closest lower fraction. I am linking it here so people can see if there are any bugs in it.)
– user2428118 – 2015-07-14T13:53:46.790Unfortunately, this means all the answers are wrong as well... – user2428118 – 2015-07-14T13:58:32.523
1Your first example does not match the specification: Both fractions must have a lower denominator than the input. – Howard – 2014-04-25T09:59:58.200
1First example, output should be
1/3 1/2
. – Heiko Oberdiek – 2014-04-25T10:02:42.007@HeikoOberdiek You're right. Fixed. – user2428118 – 2014-04-25T10:19:02.987
Could we have an example where the input is reducible? Oh, right. – isaacg – 2014-04-25T10:28:03.870
Are
0/1
and1/1
accepted if they replace0
and1
? – Michael M. – 2014-04-25T11:47:47.080@Michael
1/1
isn't a proper fraction. As for0/1
, I hadn't realized that it is a proper fraction, so I've added a rule to say that isn't allowed. – user2428118 – 2014-04-25T12:04:32.953If my language of choice uses double slashes to denote rationals, is that valid for input and output as well, or do they have to be single slashes? – Martin Ender – 2014-04-25T18:22:04.027
1Define "average home user's computer". Is 90 seconds on a 1.6GHz Intel Atom machine acceptable? – John Dvorak – 2014-04-26T06:02:02.540
Are negative fractions allowed? – user80551 – 2014-05-22T10:35:36.237
@JanDvorak I think that would be okay. I won't be too strict, if I think it's likely that it runs in about one minute on recent customer-grade hardware, I'll probably accept it (unless the chosen kind of computer isn't very common and vastly outperforms others). The reason for this rule was mainly to exclude brute-force solutions that take almost forever to complete. – user2428118 – 2014-06-02T19:50:00.860
@m.buettner I'd rather not make exceptions for specific programming languages. The rules will remain the same, even if that puts your language of choice at a disadvantage – user2428118 – 2014-06-02T19:53:56.130
@user80551 No, the rules explicitly state your program is given a proper, positive simple fraction in the format specified. – user2428118 – 2014-06-02T19:55:03.893