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$
2
  • $\begingroup$ May we assume that you are already familiar with `GCD[n1, n2,...]'? $\endgroup$
    – DavidC
    Feb 19, 2017 at 20:22
  • $\begingroup$ I think I only knew a little about `GCD[n1, n2,...]' $\endgroup$
    – Jone Will
    Feb 19, 2017 at 20:28

1 Answer 1

3
$\begingroup$

Like this:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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