The N-th Positive Rational Number
InterviewTime limit1sMemory limit256 MB
Given N, return the N-th term of the diagonal enumeration of positive rationals where each group of constant numerator plus denominator is listed with decreasing numerator.
- Level
Easy3 of 10
- Topics
- Math, Implementation, Brute force
- Solved
- No attempts yet
Problem
Minho takes a discrete mathematics course. One day the professor proved that the positive rational numbers are countable and then assigned homework.
The positive rationals are listed by the following rule. Collect the fractions whose numerator and denominator have the same sum into one group, and number the groups starting from the smallest sum. The group with sum holds fractions, and inside the group they are written from the largest numerator down: .
The listing therefore runs . The first rational is , the second is , the third is , the fourth is , and the fifth is .
Fractions of equal value such as count as separate terms. Nothing is reduced.
Minho does not want to do the homework by hand, so he writes a program instead. Find the -th rational number.
Input
The first line contains a positive integer . ()
Output
If the -th rational number is , print the numerator and the denominator on one line, separated by a space. Do not reduce the fraction.
Hint
Minho solved the homework with a program rather than by hand and derived a formula from it.