2
$\begingroup$

How can I create a function ggt[p,q],and find the greatest common divisor with Euclid's algorithm?p,q are integer.

$\endgroup$
  • $\begingroup$ May we assume that you are already familiar with `GCD[n1, n2,...]'? $\endgroup$ – DavidC Feb 19 '17 at 20:22
  • $\begingroup$ I think I only knew a little about `GCD[n1, n2,...]' $\endgroup$ – Jone Will Feb 19 '17 at 20:28
3
$\begingroup$

Like this:

ggt[p_, q_] := First[NestWhile[{Last[#], Mod @@ #} &, {p, q}, Last[#] != 0 &]]
$\endgroup$
  • $\begingroup$ Can I also build a fraction e.g. b=p/q? $\endgroup$ – Jone Will Feb 19 '17 at 20:20
  • $\begingroup$ @JoneWill The function works correctly for rational input too $\endgroup$ – Coolwater Feb 20 '17 at 7:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.