Print all proper fractions with denominators less than equal to N

Given an integer N, the task is to print all proper fractions such that the denominator is less than or equal to N. 
 

Proper Fractions: A fraction is said to be a proper fraction if the numerator is less than the denominator. 
 

Examples: 
 

Input: N = 3 
Output: 1/2, 1/3, 2/3
Input: N = 4 
Output: 1/2, 1/3, 1/4, 2/3, 3/4 
 

 



Approach: 
Traverse all numerators over [1, N-1] and, for each of them, traverse over all denominators in the range [numerator+1, N] and check if the numerator and denominator are coprime or not. If found to be coprime, then print the fraction.
Below is the implementation of the above approach:
 

C++14

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to implement the
// above approach
#include <bits/stdc++.h> 
using namespace std;
  
// Function to print all
// proper fractions
void printFractions(int n)
{
    for (int i = 1; i < n; i++) {
        for (int j = i + 1; j <= n; j++) {
  
            // If the numerator and the
            // denominator are coprime
            if (__gcd(i, j) == 1) {
  
                string a = to_string(i);
                string b = to_string(j);
  
                cout << a + "/" + b << ", ";
            }
        }
    }
}
  
// Driver Code
int main()
{
    int n = 3;
    printFractions(n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to implement the
// above approach
class GFG{
  
// Function to print all
// proper fractions
static void printFractions(int n)
{
    for(int i = 1; i < n; i++) 
    {
        for(int j = i + 1; j <= n; j++) 
        {
              
            // If the numerator and the
            // denominator are coprime
            if (__gcd(i, j) == 1)
            {
                String a = String.valueOf(i);
                String b = String.valueOf(j);
  
                System.out.print(a + "/"
                                 b + ", ");
            }
        }
    }
}
  
static int __gcd(int a, int b) 
    return b == 0 ? a : __gcd(b, a % b);     
  
// Driver code
public static void main(String[] args)
{
    int n = 3;
      
    printFractions(n);
}
}
  
// This code is contributed by 29AjayKumar

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program for the
# above approach
  
# Function to print
# all proper functions
def printfractions(n):
    
  for i in range(1, n):
    for j in range(i + 1, n + 1):
        
      # If the numerator and 
      # denominator are coprime
      if __gcd(i, j) == 1:
        a = str(i)
        b = str(j)
        print(a + '/' + b, end = ", ")
          
def __gcd(a, b):
    
  if b == 0:
    return a
  else:
    return __gcd(b, a % b)
  
# Diver code
if __name__=='__main__':
    
  n = 3
  printfractions(n)
  
# This code is contributed by virusbuddah_

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to implement the
// above approach
using System;
  
class GFG{
  
// Function to print all
// proper fractions
static void printFractions(int n)
{
    for(int i = 1; i < n; i++) 
    {
        for(int j = i + 1; j <= n; j++) 
        {
              
            // If the numerator and the
            // denominator are coprime
            if (__gcd(i, j) == 1)
            {
                string a = i.ToString();
                string b = j.ToString();
  
                Console.Write(a + "/"
                              b + ", ");
            }
        }
    }
}
  
static int __gcd(int a, int b) 
    return b == 0 ? a : __gcd(b, a % b);     
  
// Driver code
public static void Main(string[] args)
{
    int n = 3;
      
    printFractions(n);
}
}
  
// This code is contributed by rutvik_56

chevron_right


Output: 

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

 

Time Complexity: O(N2
Auxiliary Space: O(1)
 

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

Check out this Author's contributed articles.

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.