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
1/3 1/2
. \$\endgroup\$