Open In App

Split the fraction into sum of multiple fractions having numerator as 1

Last Updated : 23 Aug, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Given two positive integers N and D representing a fraction as N/D, the task is to split the fraction into the sum of multiple fractions having numerator as 1.

Examples: 

Input: n = 4, d = 5
Output: 1/2, 1/4, 1/20
Explanation: 1/2 + 1/4 + 1/20 = 4/5

Input: n = 15, d = 16
Output: 1/2, 1/3, 1/10, 1/240

Approach: The idea is that all positive fractions of the form n/d can be written as a sum of distinct unit fractions. The answer can be found by removing largest unit fraction 1/x till the fraction reaches to zero where x can be found as ceil(d/n). After finding the unit fraction, update the fraction to n/d – 1/x so n changes to nx-d and d changes to dx at each step.

Below is the implementation of the above approach: 

C++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to split the fraction into
// distinct unit fraction
vector<string> FractionSplit(long long n, long long d)
{
 
    // To store answer
    vector<string> UnitFactions;
 
    // While numerator is positive
    while (n > 0) {
 
        // Finding x = ceil(d/n)
        long long x = (d + n - 1) / n;
 
        // Add 1/x to list of ans
        string s = "1/" + to_string(x);
        UnitFactions.push_back(s);
 
        // Update fraction
        n = n * x - d;
        d = d * x;
    }
    return UnitFactions;
}
 
// Driver Code
int main()
{
    // Given Input
    long long n = 13, d = 18;
 
    // Function Call
    auto res = FractionSplit(n, d);
 
    // Print Answer
    for (string s : res)
        cout << s << ", ";
 
    return 0;
}


Java




// Java program for the above approach
import java.util.Vector;
 
public class GFG {
 
    // Function to split the fraction into
    // distinct unit fraction
    static Vector<String> FractionSplit(long n, long d)
    {
 
        // To store answer
        Vector<String> UnitFactions = new Vector<>();
 
        // While numerator is positive
        while (n > 0) {
 
            // Finding x = ceil(d/n)
            long x = (d + n - 1) / n;
 
            // Add 1/x to list of ans
            String s = "1/" + String.valueOf(x);
            UnitFactions.add(s);
 
            // Update fraction
            n = n * x - d;
            d = d * x;
        }
        return UnitFactions;
    }
 
    // Driver code
    public static void main(String[] args)
    {
       
        // Given Input
        long n = 13, d = 18;
 
        // Function Call
        Vector<String> res = FractionSplit(n, d);
 
        // Print Answer
        for (String s : res)
            System.out.print(s + ", ");
    }
}
 
// This code is contributed by abhinavjain194


Python3




# Python program for the above approach
 
# Function to split the fraction into
# distinct unit fraction
def FractionSplit(n, d):
 
    # To store answer
    UnitFactions = []
 
    # While numerator is positive
    while (n > 0):
 
        # Finding x = ceil(d/n)
        x = (d + n - 1) // n
 
        # Add 1/x to list of ans
        s = "1/" + str(x)
        UnitFactions.append(s);
 
        # Update fraction
        n = n * x - d;
        d = d * x
    return UnitFactions;
 
# Driver Code
 
# Given Input
n = 13;
d = 18;
 
# Function Call
res = FractionSplit(n, d);
 
# Print Answer
for s in res:
    print(s + ", ", end=" ");
 
# This code is contributed by _saurabh_jaiswal


C#




// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to split the fraction into
// distinct unit fraction
static List<string> FractionSplit(long n, long d)
{
     
    // To store answer
    List<string> UnitFactions = new List<string>();
 
    // While numerator is positive
    while (n > 0)
    {
         
        // Finding x = ceil(d/n)
        long x = (d + n - 1) / n;
 
        // Add 1/x to list of ans
        string s = "1/" + x.ToString();
        UnitFactions.Add(s);
 
        // Update fraction
        n = n * x - d;
        d = d * x;
    }
    return UnitFactions;
}
 
// Driver code
public static void Main(string[] args)
{
     
    // Given Input
    long n = 13, d = 18;
 
    // Function Call
    List<string> res = FractionSplit(n, d);
 
    // Print Answer
    foreach(string s in res)
        Console.Write(s + ", ");
}
}
 
// This code is contributed by ukasp


Javascript




<script>
// Javascript program for the above approach
 
// Function to split the fraction into
// distinct unit fraction
function FractionSplit(n, d) {
 
    // To store answer
    let UnitFactions = [];
 
    // While numerator is positive
    while (n > 0) {
 
        // Finding x = ceil(d/n)
        let x = Math.floor((d + n - 1) / n);
 
        // Add 1/x to list of ans
        let s = "1/" + String(x);
        UnitFactions.push(s);
 
        // Update fraction
        n = n * x - d;
        d = d * x;
    }
    return UnitFactions;
}
 
// Driver Code
 
// Given Input
let n = 13, d = 18;
 
// Function Call
let res = FractionSplit(n, d);
 
// Print Answer
for (let s of res)
    document.write(s + ", ");
 
// This code is contributed by gfgking.
</script>


Output

1/2, 1/5, 1/45, 

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

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads