Skip to content
Related Articles

Related Articles

Algorithm to generate positive rational numbers
  • Last Updated : 03 Sep, 2018

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());
    }
}


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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :