How can I create a function ggt[p,q],and find the greatest common divisor with Euclid's algorithm?p,q are integer.
$\begingroup$
$\endgroup$
2
-
$\begingroup$ May we assume that you are already familiar with `GCD[n1, n2,...]'? $\endgroup$– DavidCCommented Feb 19, 2017 at 20:22
-
$\begingroup$ I think I only knew a little about `GCD[n1, n2,...]' $\endgroup$– Jone WillCommented Feb 19, 2017 at 20:28
Add a comment
|
1 Answer
$\begingroup$
$\endgroup$
2
Like this:
ggt[p_, q_] := First[NestWhile[{Last[#], Mod @@ #} &, {p, q}, Last[#] != 0 &]]
-
$\begingroup$ Can I also build a fraction e.g. b=p/q? $\endgroup$ Commented Feb 19, 2017 at 20:20
-
$\begingroup$ @JoneWill The function works correctly for rational input too $\endgroup$ Commented Feb 20, 2017 at 7:25