Message Boards Message Boards

0
|
3052 Views
|
2 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Define myEuclid as GCD of two numbers

Posted 11 years ago
Hello,
Euklid[a0_, b0_] :=
  Module[
     {a = a0, b = b0},
     While[a*b != 0, {a, b} = {b, Mod[a, b]}];
     a
        ];

did work fine. How can I define a new function that gives me the GCD of two numbers without using the command "Module" and without using the way above when it says "{a,b} = {b, Mod[a,b]}". I mean: Without writing two commands into { and }. I separately want to tell mathematica: a = b   and   b = Mod[a,b].

Thank you for your help emoticon
POSTED BY: K L
2 Replies
Posted 11 years ago
Hi,

I'd like something that goes in this direction that uses "While" or something like this.
gcd[a_,b_] :=
      If[a==b,Print[b], While[b!=0,If[a>b,...

I just don't get it ;/
POSTED BY: K L
Not clear to me what you want. Maybe something recursive like below?
gcd[1, _] = 1;
gcd[_, 1] = 1;
gcd[a_, a_] := a
gcd[a_, b_] := gcd[b, Mod[a, b]]
POSTED BY: Daniel Lichtblau
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract