src/Pure/General/integer.ML
changeset 63227 d3ed7f00e818
parent 33029 2fefe039edf1
child 66996 22ca0f37f491
equal deleted inserted replaced
63226:d8884c111bca 63227:d3ed7f00e818
    15   val sign: int -> order
    15   val sign: int -> order
    16   val div_mod: int -> int -> int * int
    16   val div_mod: int -> int -> int * int
    17   val square: int -> int
    17   val square: int -> int
    18   val pow: int -> int -> int (* exponent -> base -> result *)
    18   val pow: int -> int -> int (* exponent -> base -> result *)
    19   val gcd: int -> int -> int
    19   val gcd: int -> int -> int
       
    20   val lcm: int -> int -> int
    20   val gcds: int list -> int
    21   val gcds: int list -> int
    21   val lcm: int -> int -> int
       
    22   val lcms: int list -> int
    22   val lcms: int list -> int
    23 end;
    23 end;
    24 
    24 
    25 structure Integer : INTEGER =
    25 structure Integer : INTEGER =
    26 struct
    26 struct
    53     if k < 0
    53     if k < 0
    54     then error "pow: negative exponent"
    54     then error "pow: negative exponent"
    55     else pw k l
    55     else pw k l
    56   end;
    56   end;
    57 
    57 
    58 fun gcd x y =
    58 fun gcd x y = PolyML.IntInf.gcd (x, y);
    59   let
    59 fun lcm x y = abs (PolyML.IntInf.lcm (x, y));
    60     fun gxd x y = if y = 0 then x else gxd y (x mod y)
       
    61   in if x < y then gxd y x else gxd x y end;
       
    62 
    60 
    63 fun gcds xs = fold gcd xs 0;
    61 fun gcds [] = 0
       
    62   | gcds (x :: xs) = fold gcd xs x;
    64 
    63 
    65 fun lcm x y = (x * y) div (gcd x y);
    64 fun lcms [] = 1
    66 fun lcms xs = fold lcm xs 1;
    65   | lcms (x :: xs) = abs (Library.foldl PolyML.IntInf.lcm (x, xs));
    67 
    66 
    68 end;
    67 end;
    69