Find the largest co-prime fraction less than the given fraction

Last Updated : 31 Jul, 2024

Consider the set of irreducible fractions A = {n/d | n≤d and d ≤ 10000 and gcd(n, d) = 1}. You are given a member of this set and your task is to find the largest fraction in this set less than the given fraction.

Note: This is a set and all the members are unique.

Examples:

Input: n = 1, d = 4
Output: {2499, 9997}  
Explanation: 2499/9997 is the largest fraction.

Input: n = 2, d = 4
Output: {4999, 9999}
Explanation: 4999/9999 is the largest fraction. 

Try It Yourself
redirect icon

Approach: The solution to the problem is based on the following mathematical concept:

Say the desired fraction is p/q. So,  

p/q < n/d
p < (n*q)/d
As we want p/q to be smaller than n/d, start to iterate over q from q = d+1.
Then for each value of q, the value of p will be floor((n*q)/d).  

Follow the below steps to implement the above idea:

  • Create two variables num and den to store the final answer. Initialize them as num =- 1 and den =1.
  • Now, loop i from d+1 to 10000:
    • Calculate the value of the fraction with denominator as i using the above observation.
    • The numerator will be (n*i)/d [this is integer division here, i.e., it gives the floor value] and denominator = i+1
    • If the fraction is greater than num/den, then update num and den accordingly.
  • After all the iterations num and den will store the required numerator and denominator.

Below is the implementation of the above approach:

C++
// C++ code to implement the approach

#include <bits/stdc++.h>
using namespace std;

// Function to find the required fraction
vector<int> numAndDen(int n, int d)
{
    // Suggested solution. This will work for all the test
    // cases.
    int num = -1, den = 1;

    for (int i = 1; i <= 10000; i++) {
        if (i != d) {
            int x = (n * i) / d;
            if (1.0 * num / den < 1.0 * x / i
                && __gcd(x, i) == 1) {
                num = x;
                den = i;
            }
        }
    }

    vector<int> v;
    v.push_back(num);
    v.push_back(den);
    return v;
}

// Driver code
int main()
{
    int n = 1, d = 4;

    // Function call
    vector<int> ans = numAndDen(n, d);
    for (auto i : ans)
        cout << i << " ";
    return 0;
}
Java
// Java code to implement the approach
import java.io.*;
import java.util.*;

class GFG {
    public static int gcd(int a, int b)
    {
        if (b == 0)
            return a;
        return gcd(b, a % b);
    }

    // Function to find the required fraction
    public static ArrayList<Integer> numAndDen(int n, int d)
    {
        int num = -1;
        int den = 1;
        ArrayList<Integer> ans = new ArrayList<Integer>();

        // Loop to find the fraction
        for (int i = d + 1; i <= 10000; i++) {
            int x = (n * i) / d;
            if (1.0 * x / i > 1.0 * num / den
                && gcd(x, i) == 1) {
                num = x;
                den = i;
            }
        }
        ans.add(num);
        ans.add(den);
        return ans;
    }

    // Driver Code
    public static void main(String[] args)
    {
        int n = 1, d = 4;

        // Function call
        ArrayList<Integer> ans = numAndDen(n, d);
        for (Integer i : ans)
            System.out.print(i + " ");
    }
}

// This code is contributed by Rohit Pradhan
Python
# Python3 code to implement the approach

from math import gcd

# Function to find the required fraction
def numAndDen(n, d) :

    num = -1; den = 1;
    ans = [];

    # Loop to find the fraction
    for i in range(d + 1, 10001) :
        x = (n * i) // d;
        if ((1.0 * x / i) > (1.0 * num / den) and gcd(x, i) == 1) :
            num = x;
            den = i;
            
    ans.append(num);
    ans.append(den);
    
    return ans;

# Driver code
if __name__ ==  "__main__" :

    n = 1; d = 4;

    # Function call
    ans = numAndDen(n, d);
    
    for i in ans:
        print(i,end=" ");

    # This code is contributed by AnkThon
C#
// C# code to implement the approach
using System;
using System.Collections;
public class GFG{
  static int gcd(int a, int b)
  {
    if (b == 0)
      return a;
    return gcd(b, a % b);
  }

  // Function to find the required fraction
  static ArrayList numAndDen(int n, int d)
  {
    int num = -1;
    int den = 1;
    ArrayList ans = new ArrayList(); 

    // Loop to find the fraction
    for (int i = d + 1; i <= 10000; i++) {
      int x = (n * i) / d;
      if (1.0 * x / i > 1.0 * num / den
          && gcd(x, i) == 1) {
        num = x;
        den = i;
      }
    }
    ans.Add(num);
    ans.Add(den);
    return ans;
  }

  // Driver Code
  static public void Main (){
    int n = 1, d = 4;

    // Function call
    ArrayList ans = numAndDen(n, d);
    foreach(var i in ans)
      Console.Write(i + " ");
  }
}

// This code is contributed by hrithikgarg03188.
JavaScript
        // JavaScript code for the above approach
        function __gcd(a, b) {
            if (b == 0)
                return a;
            return __gcd(b, a % b);
        }
        
        // Function to find the required fraction
        function numAndDen(n, d) {
            let num = -1, den = 1;
            let ans = [];

            // Loop to find the fraction
            for (let i = d + 1; i <= 10000; i++) {
                let x = Math.floor((n * i) / d);
                if (1.0 * (x / i) > 1.0 * (num / den)
                    && __gcd(x, i) == 1)
                    num = x, den = i;
            }
            ans.push(num);
            ans.push(den);
            return ans;
        }

        // Driver code
        let n = 1, d = 4;

        // Function call
        let ans = numAndDen(n, d);
        for (let i of ans)
            console.log(i + " ");

Output
2499 9997 

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

Comment