Skip to content
Related Articles

Related Articles

Print all proper fractions with denominators less than equal to N
  • Last Updated : 22 Mar, 2021
GeeksforGeeks - Summer Carnival Banner

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




// 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;
}

Java




// 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

Python3




# 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)
 
# Driver code
if __name__=='__main__':
   
  n = 3
  printfractions(n)
 
# This code is contributed by virusbuddah_

C#




// 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

Javascript




<script>
// Javascript program for the above approach
  
// Function to print all proper functions
const printFractions = (n) => {
  for (var i = 1; i < n; i++) {
      for (var j = i + 1; j <= n; j++) {
       
      // If the numerator and denominator are coprime
       
      if(__gcd(i, j) == 1){
        let a = `${i}`;
        let b = `${j}`;
        document.write(`${a}/${b}, `)
      }
    }
  }
}
 
const __gcd = (a, b) => {
  if(b == 0){
    return a;
  }else{
    return __gcd(b, a % b);
  }
}
 
// Driver code
let n = 3;
printFractions(n);
 
// This aricle is contributed by _saurabh_jaiswal
</script>
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
Recommended Articles
Page :