Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

fractions

Status
Not open for further replies.

michaela1

Programmer
Mar 22, 2008
2
IE
Hi
I am trying to write predicates to process the following
simplify([[1,2,3], plus, [3,4,5]],X).
X = [5,7,15]

any tips welcome?
below is my code so far

1 2/3 + 3 4/5

5/3 + 19/5

(25+57)/15 = 82/15 = 5 7/15

format [1,2,3] to 5/3

multiply ([1,2,3],[3,0,0], [X,Y,Z]).
X=[5]
Y=[0]
Z=[0]

convert [3,4,5] to 19/5
Multiply ([3,4,5], [5,0,0], [X,Y,Z]).
X=[19]
Y=[0]
Z=[0]

gcd(a,b)= gcd(b, a mod b)

sum(N1, D1, N2, D2, N4, D4) :-
N3 is N1*D2 + N2*D1, %numerator
D3 is D1*D2, %denominator
gcd(N3, D3, GCD), %Simplify
N4 is N3/GCD
D4 is D3/GCD

Sum(5, 3, 19, 5, N4, D4) :-
N3 is (5*5) + (19*3),
D3 is 3*5,
gcd(82, 15, GCD),
N4 is 82/GCD,
D4 is 15/GCD.

N4 becomes 82, D4 becomes 15. Numbers are as simple as possible.

simplify([L1, plus, L2], X) =
convert(L1, R1), convert(L2, R2)
Add (R1, R2, X).

simplify([[1,2,3], plus [3,4,5]], X) =
convert (1,2,3, R1), convert( 3,4,5, R2)
add( R1,R2, X).


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top