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