# Algorithm to generate positive rational numbers

A rational number is of the form p/q where p and q are integers. The problem statement is to generate rational number such that any particular number is generated in a finite time. For a given n, we generate all rational numbers where 1 <= p <= n and 1 <= q <= n

Examples:

Input : 5 Output : 1, 1/2, 2, 1/3, 2/3, 3/2, 3, 1/4, 3/4, 4/3, 4, 1/5, 2/5, 3/5, 4/5, 5/4, 5/3, 5/2, 5 Input : 7 Output :1, 1/2, 2, 1/3, 2/3, 3/2, 3, 1/4, 3/4, 4/3, 4, 1/5, 2/5, 3/5, 4/5, 5/4, 5/3, 5/2, 5, 1/6, 5/6, 6/5, 6, 1/7, 2/7, 3/7, 4/7, 5/7, 6/7, 7/6, 7/5, 7/4, 7/3, 7/2, 7

In mathematical terms a set is countably infinite if its elements can be mapped on a one to one basis with the set of natural numbers.

The problem statement here is to generate combinations of p/q where both p and q are integers and any particular combination of p and q will be reached in a finite no. of steps. If p is incremented 1, 2, 3… etc keeping q constant or vice versa all combinations cannot be reached in finite time. The way to handle this is to imagine the natural numbers arranged as a row, col of a matrix

(1, 1) (1, 2) (1, 3) (1, 4)

(2, 1) (2, 2) (2, 3) (2, 4)

(3, 1) (3, 2) (3, 3) (3, 4)

(4, 1) (4, 2) (4, 3) (4, 4)

These elements are traversed in an **inverted** L shape in each iteration

(1, 1)

(1, 2), (2, 2) (2, 1)

(1, 3), (2, 3), (3, 3), (3, 2), (3, 1)

yielding

1/1

1/2, 2/2, 2/1

1/3, 2/3, 3/3, 3/2, 3/1

Obviously this will yield duplicates as 2/1 and 4/2 etc, but these can be weeded out by using the Greatest common divisor constraint.

`// Java program ` `import` `java.util.ArrayList; ` `import` `java.util.List; ` ` ` `class` `Rational { ` ` ` ` ` `private` `static` `class` `RationalNumber { ` ` ` ` ` `private` `int` `numerator; ` ` ` `private` `int` `denominator; ` ` ` ` ` `public` `RationalNumber(` `int` `numerator, ` `int` `denominator) ` ` ` `{ ` ` ` `this` `.numerator = numerator; ` ` ` `this` `.denominator = denominator; ` ` ` `} ` ` ` ` ` `@Override` ` ` `public` `String toString() ` ` ` `{ ` ` ` `if` `(denominator == ` `1` `) { ` ` ` `return` `Integer.toString(numerator); ` ` ` `} ` ` ` `else` `{ ` ` ` `return` `Integer.toString(numerator) + ` `'/'` `+ ` ` ` `Integer.toString(denominator); ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `/** ` ` ` `* Greatest common divisor ` ` ` `* @param num1 ` ` ` `* @param num2 ` ` ` `* @return ` ` ` `*/` ` ` `private` `static` `int` `gcd(` `int` `num1, ` `int` `num2) ` ` ` `{ ` ` ` `int` `n1 = num1; ` ` ` `int` `n2 = num2; ` ` ` ` ` `while` `(n1 != n2) { ` ` ` `if` `(n1 > n2) ` ` ` `n1 -= n2; ` ` ` `else` ` ` `n2 -= n1; ` ` ` `} ` ` ` `return` `n1; ` ` ` `} ` ` ` ` ` `private` `static` `List<RationalNumber> generate(` `int` `n) ` ` ` `{ ` ` ` ` ` `List<RationalNumber> list = ` `new` `ArrayList<>(); ` ` ` ` ` `if` `(n > ` `1` `) { ` ` ` `RationalNumber rational = ` `new` `RationalNumber(` `1` `, ` `1` `); ` ` ` `list.add(rational); ` ` ` `} ` ` ` ` ` `for` `(` `int` `loop = ` `1` `; loop <= n; loop++) { ` ` ` ` ` `int` `jump = ` `1` `; ` ` ` ` ` `// Handle even case ` ` ` `if` `(loop % ` `2` `== ` `0` `) ` ` ` `jump = ` `2` `; ` ` ` `else` ` ` `jump = ` `1` `; ` ` ` ` ` `for` `(` `int` `row = ` `1` `; row <= loop - ` `1` `; row += jump) { ` ` ` ` ` `// Add only if there are no common divisors other than 1 ` ` ` `if` `(gcd(row, loop) == ` `1` `) { ` ` ` `RationalNumber rational = ` `new` `RationalNumber(row, loop); ` ` ` `list.add(rational); ` ` ` `} ` ` ` `} ` ` ` ` ` `for` `(` `int` `col = loop - ` `1` `; col >= ` `1` `; col -= jump) { ` ` ` ` ` `// Add only if there are no common divisors other than 1 ` ` ` `if` `(gcd(col, loop) == ` `1` `) { ` ` ` `RationalNumber rational = ` `new` `RationalNumber(loop, col); ` ` ` `list.add(rational); ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `list; ` ` ` `} ` ` ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `List<RationalNumber> rationals = generate(` `7` `); ` ` ` `System.out.println(rationals.stream(). ` ` ` `map(RationalNumber::toString). ` ` ` `reduce((x, y) -> x + ` `", "` `+ y).get()); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

**Output:**

1, 1/2, 2, 1/3, 2/3, 3/2, 3, 1/4, 3/4, 4/3, 4, 1/5, 2/5, 3/5, 4/5, 5/4, 5/3, 5/2, 5, 1/6, 5/6, 6/5, 6, 1/7, 2/7, 3/7, 4/7, 5/7, 6/7, 7/6, 7/5, 7/4, 7/3, 7/2, 7

## Recommended Posts:

- New Algorithm to Generate Prime Numbers from 1 to Nth Number
- Find max of two Rational numbers
- Find LCM of rational numbers
- HCF of array of fractions (or rational numbers)
- N-th term of George Cantor set of rational numbers
- Generate all palindromic numbers less than n
- Count ordered pairs of positive numbers such that their sum is S and XOR is K
- Nicomachus’s Theorem (Sum of k-th group of odd positive numbers)
- Generate k digit numbers with digits in strictly increasing order
- Generate a list of n consecutive composite numbers (An interesting method)
- Find HCF of two numbers without using recursion or Euclidean algorithm
- Maximum rational number (or fraction) from an array
- Minimum positive integer value possible of X for given A and B in X = P*A + Q*B
- Generate all rotations of a number
- Generate Pythagorean Triplets

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.