Friday, January 10, 2025
Google search engine
HomeData Modelling & AIPrint all proper fractions with denominators less than equal to N

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




// 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 article is contributed by _saurabh_jaiswal
</script>


Output: 

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

 

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

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments