Find N fractions that sum upto a given fraction N/D

Given a fraction N/D, the task is to split this fraction into N parts such that their sum is equal to the fraction N/D, i.e.,
\frac{N}{D} = \frac{a_1}{b_1} + \frac{a_2}{b_2} + ... + \frac{a_N}{b_N}

Note: Represents the terms in terms of fractions, instead of floating point numbers.

Input: N = 4, D = 2
Output: 4/5, 1/5, 1/3, 4/6
Explanation:
\frac{4}{5} + \frac{1}{5} + \frac{1}{3} + \frac{4}{6} = \frac{4}{2}
Therefore, it is a valid set of fractions such that their sum is \frac{N}{D}

Input: N = 3, D = 4
Output: 1/2, 1/10, 3/20
Explanation:
\frac{1}{2} + \frac{1}{10} + \frac{3}{20} = \frac{3}{4}
Therefore, it is a valid set of fractions such that their sum is \frac{N}{D}

Approach: The key observation in the problem is that the first fraction numerator can be D + N - 1 and then further N-1 denominators can be using the below recurrence relation.



(i+1)^{th} Denominator = i^{th} Denominator * (i^{th} Denominator - 1)

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation to split the
// fraction into N parts
#include<bits/stdc++.h>
using namespace std;
  
// Function to split the fraction
// into the N parts
void splitFraction(int n, int d)
{
    int ar[n];
    int first = d + n - 1;
    ar[0] = first;
  
    // Loop to find the N - 1
    // fraction
    for(int i = 1; i < n; i++)
    {
       int temp = --first;
       first++;
  
       ar[i] = first * temp;
       --first;
    }
  
    // Loop to print the Fractions
    for(int i = 0; i < n; i++)
    {
       if (ar[i] % n == 0) 
       {
           cout << "1/" << ar[i] / n << ", ";
       }
       else 
       {
           cout << n << "/" << ar[i] << ", ";
       }
    }
}
  
// Driver Code
int main()
{
    int N = 4;
    int D = 2;
  
    // Function Call
    splitFraction(N, D);
}
  
// This code is contributed by Bhupendra_Singh

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation to split the
// fraction into N parts
  
import java.util.Scanner;
  
class Solution {
  
    // Function to split the fraction
    // into the N parts
    public static void
    splitFraction(int n, int d)
    {
  
        long ar[] = new long[n];
        long first = d + n - 1;
        ar[0] = first;
  
        // Loop to find the N - 1
        // fraction
        for (int i = 1; i < n; i++) {
            ar[i] = first * (--first);
        }
  
        // Loop to print the Fractions
        for (int i = 0; i < n; i++) {
            if (ar[i] % n == 0) {
                System.out.print(
                    "1/" + ar[i] / n
                    + ", ");
            }
            else {
                System.out.print(
                    n + "/" + ar[i]
                    + ", ");
            }
        }
    }
  
    // Driver Code
    public static void main(
        String[] args) throws Exception
    {
        int N = 4;
        int D = 2;
  
        // Function Call
        splitFraction(N, D);
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation to split the 
# fraction into N parts 
  
# Function to split the fraction 
# into the N parts 
def splitFraction(n, d):
      
    ar = []
    for i in range(0, n): 
        ar.append(0)
      
    first = d + n - 1
    ar[0] = first
      
    # Loop to find the N - 1 
    # fraction
    for i in range(1, n):
        temp = first - 1
        ar[i] = first * temp
        first -= 1
      
    # Loop to print the Fractions 
    for i in range(0, n):
        if ar[i] % n == 0:
            print("1/", int(ar[i] / n),
                  "," , end = " ")
                    
        else:
            print(n, "/", ar[i], ",", end = " ")
      
# Driver Code 
N = 4
D = 2
  
# Function Call 
splitFraction(N, D)
  
# This code is contributed by ishayadav181

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation to split the
// fraction into N parts
using System;
  
class GFG{
  
// Function to split the fraction
// into the N parts
public static void splitFraction(int n, int d)
{
    long []ar = new long[n];
    long first = d + n - 1;
    ar[0] = first;
  
    // Loop to find the N - 1
    // fraction
    for(int i = 1; i < n; i++)
    {
       ar[i] = first * (--first);
    }
  
    // Loop to print the Fractions
    for(int i = 0; i < n; i++) 
    {
       if (ar[i] % n == 0)
       {
           Console.Write("1/" + ar[i] / n + ", ");
       }
       else
       {
           Console.Write(n + "/" + ar[i] + ", ");
       }
    }
}
  
// Driver Code
public static void Main(String[] args) 
{
    int N = 4;
    int D = 2;
  
    // Function Call
    splitFraction(N, D);
}
}
  
// This code is contributed by SoumikMondal

chevron_right


Output:

4/5, 1/5, 1/3, 4/6,

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.