Split the fraction into sum of multiple fractions having numerator as 1 | Java 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
import java . util . Vector ; public class GFG { static Vector < String > FractionSplit ( long n , long d ) { Vector < String > UnitFactions = new Vector < > ( ) ; while ( n > 0 ) { long x = ( d + n - 1 ) / n ; String s = "1 / " + String . valueOf ( x ) ; UnitFactions . add ( s ) ; n = n * x - d ; d = d * x ; } return UnitFactions ; } public static void main ( String [ ] args ) { long n = 13 , d = 18 ; Vector < String > res = FractionSplit ( n , d ) ; for ( String s : res ) System . out . print ( s + " , ▁ " ) ; } }