/* Created by : Tan.
 
 All 3 in one module!
 
 1: Reduces the Fractions into Lowest terms
 
 2: Finds the LCM in 2 denomintors
 
 3: Adds mixed fractions and results the answer in lowest terms!
 
 */
 
 
module Maths  %% Module
 
 
    export LCM, fractionAdd, Reduce %% .LCM
 
 
    fcn Reduce (whole : int, numerator : int, denominator : int) : string    %%% REDUCTION PROCEDURE
 
 
        var num := (denominator * whole) + numerator %% NUMERATOR
 
        var den := denominator %% DENOMINATOR
 
        var numorden : int
 
        var ans : string %% ANSWER
 
 
        if num mod den = 0 then %% IF IT IS A LIKE FRACTION
 
            ans := intstr (num div den) + "/" + "1"
 
            %%put ans
 
        else % IF IT IS AN UNLIKE FRACTION
 
 
            if den < num then   %% IF THE DENOMINATOR IS GREATOR THEN WE TAKE THE GCF FROM DECREASING DENOMINATOR
 
                numorden := den
 
            elsif den > num then %% VICE VERSA
 
                numorden := num
 
            end if
 
 
            for decreasing s : numorden .. 1 %% GREATEST COMMON FACTOR
 
                if (num mod s = 0) and (den mod s = 0) then
 
                    numorden := s %% NUM OR DEN NOW BECOMES THE GCF
 
                    exit
 
                end if
 
            end for
 
 
            if den > num then  %% ANSWERS
 
                %% IF IT CANT BE REDUCED MORE THEN
 
                ans := intstr (num div numorden) + "/" + intstr (den div numorden)
 
            elsif den < num then
 
                %% IF IT CAN BE REDUCED, EXPRESS IT AS A MIXED FRACTION
 
                ans := intstr ((num div numorden) div (den div numorden)) + " " +
 
                    intstr ((num div numorden) - (((num div numorden) div (den div numorden)) * den div numorden)) + "/" + intstr (den div numorden)
 
            end if
 
        end if
 
 
        result ans %% PUTTING THE MAIN ANSWER WE GOT
 
 
    end Reduce %% ENDING THE REDUCE
 
 
 
    %%%%% Finds the LCM %%%%%
 
    fcn LCM (denominator1 : int, denominator2 : int) : int %% Main Function
 
 
        var choice, ans : int %choice detemines whether to start counting from numerator or denominator
 
 
        if denominator1 > denominator2 then
 
            choice := denominator1 %if numerator is bigger then choice = numerator
 
        elsif denominator1 < denominator2 then
 
            choice := denominator2 %elsif it = denominator
 
        else
 
            choice := denominator1 %% both are equal
 
        end if
 
 
        loop
 
            exit when (choice mod denominator1 = 0) and (choice mod denominator2 = 0)
 
            choice += 1
 
        end loop
 
 
        ans := choice
 
 
        result ans
 
 
    end LCM %% Lcm Ends
 
 
    %%%%%% Adds the Fractions and then reduces them to lowest terms%%%%%%
 
    fcn fractionAdd (whole1 : int, num1 : int, den1 : int, whole2 : int, num2 : int, den2 : int) : string
 
        var wholetotal := whole1 + whole2
 
        var lcm := LCM (den1, den2)
 
        var numerator := ((lcm div den1) * num1) + ((lcm div den2) * num2)
 
        result Reduce (wholetotal, numerator, lcm)
 
    end fractionAdd
 
 
end Maths %% End Module
 
 
put Maths.Reduce (0, 10, 25) %Whole num, Numerator, denominator
 
put skip, Maths.LCM (23, 99) % Denominator1, Denominator2
 
put skip, Maths.fractionAdd (2, 7, 5, 3, 2, 3) %Whole1, Numerator1, Denominator1, Whole2, Numerator2, Denominator2
 
  |