Greatest common divisor: Difference between revisions

m
Line 1,036:
(*
 
GCD of two integers, by Euler’sEuclid’s algorithm; verified with proofs.
 
Compile with ‘patscc -o gcd gcd.dats’.
Line 1,104:
(* *)
 
(* g1uint_gcd_eulerg1uint_gcd_euclid will be the generic template function for
unsigned integers. *)
extern fun {tk : tkind}
g1uint_gcd_euclid :
g1uint_gcd_euler :
{u, v : int}
(g1uint (tk, u),
Line 1,113:
g1uint (tk, gcd (u, v))
 
(* g1int_gcd_eulerg1int_gcd_euclid will be the generic template function for
signed integers, giving an unsigned result. *)
extern fun {tk_signed, tk_unsigned : tkind}
g1int_gcd_euclid :
g1int_gcd_euler :
{u, v : int}
(g1int (tk_signed, u),
Line 1,122:
g1uint (tk_unsigned, gcd (u, v))
 
(* Let us call these functions ‘gcd_euler’‘gcd_euclid’ or just ‘gcd’. *)
overload gcd_eulergcd_euclid with g1uint_gcd_eulerg1uint_gcd_euclid
overload gcd_eulergcd_euclid with g1int_gcd_eulerg1int_gcd_euclid
overload gcd with gcd_eulergcd_euclid
 
(********************************************************************)
Line 1,149:
 
implement {tk}
g1uint_gcd_eulerg1uint_gcd_euclid {u, v} (u, v) =
let
(* The static variable v, which is defined within the curly
Line 1,220:
 
implement {tk_signed, tk_unsigned}
g1int_gcd_eulerg1int_gcd_euclid {u, v} (u, v) =
let
(* Prove that gcd(abs u, abs v) equals gcd(u, v). *)
Line 1,227:
(* Compute gcd(abs u, abs v). The ‘g1i2u’ notations cast the
values from signed integers to unsigned integers. *)
g1uint_gcd_eulerg1uint_gcd_euclid (g1i2u (abs u), g1i2u (abs v))
end
 
Line 1,264:
 
(********************************************************************)</lang>
 
 
===Some proofs about the gcd===
1,448

edits