Finding the GCD of three numbers?Is it correct to write gcd(a,b) if a<b?Recursive RelationsExtended Euclidean Algorithm with negative numbers minimum non-negative solutionPairs comparisons between two listsapproximate greatest common divisorAlgorithm for finding pairs in a list.Condition on prime Factorizations of 2 relatively prime numbersIs there a way I could express gcd as a linear combination of two numbers in more than 1 way?How $gcd (s,t)=1$ implies $gcd(s-t,s+t) =1$?GCD algorithm with approximative factors

Why aren't there more Gauls like Obelix?

Was this cameo in Captain Marvel computer generated?

Are small insurances worth it?

ESPP--any reason not to go all in?

Limpar string com Regex

How do you make a gun that shoots melee weapons and/or swords?

How does a sound wave propagate?

Can I negotiate a patent idea for a raise, under French law?

Why restrict private health insurance?

Is there a logarithm base for which the logarithm becomes an identity function?

I am the person who abides by rules but breaks the rules . Who am I

What is better: yes / no radio, or simple checkbox?

How does learning spells work when leveling a multiclass character?

What do you call someone who likes to pick fights?

How to educate team mate to take screenshots for bugs with out unwanted stuff

Tabular environment - text vertically positions itself by bottom of tikz picture in adjacent cell

Use Mercury as quenching liquid for swords?

Is there a math expression equivalent to the conditional ternary operator?

Does the US political system, in principle, allow for a no-party system?

Mixed Feelings - What am I

Does an unused member variable take up memory?

Propulsion Systems

Is the differential, dp, exact or not?

3.5% Interest Student Loan or use all of my savings on Tuition?



Finding the GCD of three numbers?


Is it correct to write gcd(a,b) if a<b?Recursive RelationsExtended Euclidean Algorithm with negative numbers minimum non-negative solutionPairs comparisons between two listsapproximate greatest common divisorAlgorithm for finding pairs in a list.Condition on prime Factorizations of 2 relatively prime numbersIs there a way I could express gcd as a linear combination of two numbers in more than 1 way?How $gcd (s,t)=1$ implies $gcd(s-t,s+t) =1$?GCD algorithm with approximative factors













11












$begingroup$


I have found an algorithm called the Binary GCD/Stein Algorithm which takes two non-negative integers and finds the greatest common factor of the two.



What I am trying to do is take three numbers and find if each pair is relatively prime (where the GCD is one). For example, if I have non-negative variables a b and c, then:



a = 1
b = 1
c = 2


Would satisfy this because none of these numbers have a GCD that is larger than one. Using a variation of the Stein Algorithm in Java, I would like to be able to find the GCD of all three pairs of numbers (ab bc ac) with each side being able to go up to five, for the sake of simplicity.



Is there a way I could tweak the algorithm to compute the GCD of three numbers, or would I have to compute three groups of two pairs?



Thanks for any and all help.










share|cite|improve this question











$endgroup$











  • $begingroup$
    Please note, there is an example of the implementation of this algorithm, in Java, on the link above.
    $endgroup$
    – nmagerko
    Dec 23 '11 at 18:07










  • $begingroup$
    If you treat $gcd$ as an operator, much like addition or multiplication, it's commutative and associative.
    $endgroup$
    – J. M. is not a mathematician
    Dec 23 '11 at 18:17










  • $begingroup$
    you could call your gcd function twice $d=rm gcd(a,b)$, $e=rm gcd(d,c)$
    $endgroup$
    – yoyo
    Dec 23 '11 at 18:19










  • $begingroup$
    It may be commutative if I have two of the same number, but if the sides are 11,6,1, I don't see how they could be commutative. Maybe I am misunderstanding your comment?
    $endgroup$
    – nmagerko
    Dec 23 '11 at 18:20






  • 3




    $begingroup$
    $gcd(a,b,c) = gcd(gcd(a,b),c) = gcd(a,gcd(b,c))=gcd(gcd(a,c),b)$
    $endgroup$
    – Álvaro Lozano-Robledo
    Dec 23 '11 at 18:21















11












$begingroup$


I have found an algorithm called the Binary GCD/Stein Algorithm which takes two non-negative integers and finds the greatest common factor of the two.



What I am trying to do is take three numbers and find if each pair is relatively prime (where the GCD is one). For example, if I have non-negative variables a b and c, then:



a = 1
b = 1
c = 2


Would satisfy this because none of these numbers have a GCD that is larger than one. Using a variation of the Stein Algorithm in Java, I would like to be able to find the GCD of all three pairs of numbers (ab bc ac) with each side being able to go up to five, for the sake of simplicity.



Is there a way I could tweak the algorithm to compute the GCD of three numbers, or would I have to compute three groups of two pairs?



Thanks for any and all help.










share|cite|improve this question











$endgroup$











  • $begingroup$
    Please note, there is an example of the implementation of this algorithm, in Java, on the link above.
    $endgroup$
    – nmagerko
    Dec 23 '11 at 18:07










  • $begingroup$
    If you treat $gcd$ as an operator, much like addition or multiplication, it's commutative and associative.
    $endgroup$
    – J. M. is not a mathematician
    Dec 23 '11 at 18:17










  • $begingroup$
    you could call your gcd function twice $d=rm gcd(a,b)$, $e=rm gcd(d,c)$
    $endgroup$
    – yoyo
    Dec 23 '11 at 18:19










  • $begingroup$
    It may be commutative if I have two of the same number, but if the sides are 11,6,1, I don't see how they could be commutative. Maybe I am misunderstanding your comment?
    $endgroup$
    – nmagerko
    Dec 23 '11 at 18:20






  • 3




    $begingroup$
    $gcd(a,b,c) = gcd(gcd(a,b),c) = gcd(a,gcd(b,c))=gcd(gcd(a,c),b)$
    $endgroup$
    – Álvaro Lozano-Robledo
    Dec 23 '11 at 18:21













11












11








11


4



$begingroup$


I have found an algorithm called the Binary GCD/Stein Algorithm which takes two non-negative integers and finds the greatest common factor of the two.



What I am trying to do is take three numbers and find if each pair is relatively prime (where the GCD is one). For example, if I have non-negative variables a b and c, then:



a = 1
b = 1
c = 2


Would satisfy this because none of these numbers have a GCD that is larger than one. Using a variation of the Stein Algorithm in Java, I would like to be able to find the GCD of all three pairs of numbers (ab bc ac) with each side being able to go up to five, for the sake of simplicity.



Is there a way I could tweak the algorithm to compute the GCD of three numbers, or would I have to compute three groups of two pairs?



Thanks for any and all help.










share|cite|improve this question











$endgroup$




I have found an algorithm called the Binary GCD/Stein Algorithm which takes two non-negative integers and finds the greatest common factor of the two.



What I am trying to do is take three numbers and find if each pair is relatively prime (where the GCD is one). For example, if I have non-negative variables a b and c, then:



a = 1
b = 1
c = 2


Would satisfy this because none of these numbers have a GCD that is larger than one. Using a variation of the Stein Algorithm in Java, I would like to be able to find the GCD of all three pairs of numbers (ab bc ac) with each side being able to go up to five, for the sake of simplicity.



Is there a way I could tweak the algorithm to compute the GCD of three numbers, or would I have to compute three groups of two pairs?



Thanks for any and all help.







elementary-number-theory algorithms






share|cite|improve this question















share|cite|improve this question













share|cite|improve this question




share|cite|improve this question








edited Dec 23 '11 at 18:17









J. M. is not a mathematician

61.3k5152290




61.3k5152290










asked Dec 23 '11 at 18:06









nmagerkonmagerko

2752515




2752515











  • $begingroup$
    Please note, there is an example of the implementation of this algorithm, in Java, on the link above.
    $endgroup$
    – nmagerko
    Dec 23 '11 at 18:07










  • $begingroup$
    If you treat $gcd$ as an operator, much like addition or multiplication, it's commutative and associative.
    $endgroup$
    – J. M. is not a mathematician
    Dec 23 '11 at 18:17










  • $begingroup$
    you could call your gcd function twice $d=rm gcd(a,b)$, $e=rm gcd(d,c)$
    $endgroup$
    – yoyo
    Dec 23 '11 at 18:19










  • $begingroup$
    It may be commutative if I have two of the same number, but if the sides are 11,6,1, I don't see how they could be commutative. Maybe I am misunderstanding your comment?
    $endgroup$
    – nmagerko
    Dec 23 '11 at 18:20






  • 3




    $begingroup$
    $gcd(a,b,c) = gcd(gcd(a,b),c) = gcd(a,gcd(b,c))=gcd(gcd(a,c),b)$
    $endgroup$
    – Álvaro Lozano-Robledo
    Dec 23 '11 at 18:21
















  • $begingroup$
    Please note, there is an example of the implementation of this algorithm, in Java, on the link above.
    $endgroup$
    – nmagerko
    Dec 23 '11 at 18:07










  • $begingroup$
    If you treat $gcd$ as an operator, much like addition or multiplication, it's commutative and associative.
    $endgroup$
    – J. M. is not a mathematician
    Dec 23 '11 at 18:17










  • $begingroup$
    you could call your gcd function twice $d=rm gcd(a,b)$, $e=rm gcd(d,c)$
    $endgroup$
    – yoyo
    Dec 23 '11 at 18:19










  • $begingroup$
    It may be commutative if I have two of the same number, but if the sides are 11,6,1, I don't see how they could be commutative. Maybe I am misunderstanding your comment?
    $endgroup$
    – nmagerko
    Dec 23 '11 at 18:20






  • 3




    $begingroup$
    $gcd(a,b,c) = gcd(gcd(a,b),c) = gcd(a,gcd(b,c))=gcd(gcd(a,c),b)$
    $endgroup$
    – Álvaro Lozano-Robledo
    Dec 23 '11 at 18:21















$begingroup$
Please note, there is an example of the implementation of this algorithm, in Java, on the link above.
$endgroup$
– nmagerko
Dec 23 '11 at 18:07




$begingroup$
Please note, there is an example of the implementation of this algorithm, in Java, on the link above.
$endgroup$
– nmagerko
Dec 23 '11 at 18:07












$begingroup$
If you treat $gcd$ as an operator, much like addition or multiplication, it's commutative and associative.
$endgroup$
– J. M. is not a mathematician
Dec 23 '11 at 18:17




$begingroup$
If you treat $gcd$ as an operator, much like addition or multiplication, it's commutative and associative.
$endgroup$
– J. M. is not a mathematician
Dec 23 '11 at 18:17












$begingroup$
you could call your gcd function twice $d=rm gcd(a,b)$, $e=rm gcd(d,c)$
$endgroup$
– yoyo
Dec 23 '11 at 18:19




$begingroup$
you could call your gcd function twice $d=rm gcd(a,b)$, $e=rm gcd(d,c)$
$endgroup$
– yoyo
Dec 23 '11 at 18:19












$begingroup$
It may be commutative if I have two of the same number, but if the sides are 11,6,1, I don't see how they could be commutative. Maybe I am misunderstanding your comment?
$endgroup$
– nmagerko
Dec 23 '11 at 18:20




$begingroup$
It may be commutative if I have two of the same number, but if the sides are 11,6,1, I don't see how they could be commutative. Maybe I am misunderstanding your comment?
$endgroup$
– nmagerko
Dec 23 '11 at 18:20




3




3




$begingroup$
$gcd(a,b,c) = gcd(gcd(a,b),c) = gcd(a,gcd(b,c))=gcd(gcd(a,c),b)$
$endgroup$
– Álvaro Lozano-Robledo
Dec 23 '11 at 18:21




$begingroup$
$gcd(a,b,c) = gcd(gcd(a,b),c) = gcd(a,gcd(b,c))=gcd(gcd(a,c),b)$
$endgroup$
– Álvaro Lozano-Robledo
Dec 23 '11 at 18:21










3 Answers
3






active

oldest

votes


















9












$begingroup$

As others say, one way to do it is using the identity $gcd(a,b,c)=gcd(a,(gcd(b,c))$.
This identity is true since the "gcd" is the maximal element of the intersection of the sets of factors of the inputs. For example, taking $gcd(6,10)$, the set of factors of $6$ is $6, 3, 2, 1$, the set of factors of $10$ is $10, 5, 2, 1$, their intersection is $2, 1$, and the maximal element is $2$. The identity follows since intersections of sets are commutative and associative.



However, it's also possible to extend Stein's algorithm to handle more than two inputs. Wikipedia gives 5 steps to Stein's algorithm, and we can change them to the following to compute the gcd of a set $S$ of numbers:



  1. If anything in $S$ is zero, remove it.

  2. If everything in $S$ is even, divide everything in $S$ by 2, and then multiply the final answer by 2 at the end.

  3. If some numbers in $S$ are even and some are odd, divide all the even numbers by 2.

  4. If every number in $S$ is odd, then choose an arbitrary element of $S$ and call it $k$. Replace every other element, say $n$, with $|n-k|/2$.

  5. Repeat steps 1–4 until there is only one remaining element in $S$. After you remember to multiply in the twos that you got from step 2, that's your gcd!





share|cite|improve this answer











$endgroup$












  • $begingroup$
    I do believe that this algorithm takes the minimal element of the intersection, doesn't it?
    $endgroup$
    – nmagerko
    Dec 23 '11 at 19:26






  • 1




    $begingroup$
    This answer has a mistake in it! In step 4, one should choose the smallest element, and not an arbitrary one. I have tried to edit the answer twice, both tries were rejected.
    $endgroup$
    – AmiguelS
    Jan 23 '18 at 14:54



















0












$begingroup$

Here is J.M. s algorithm implemented as a c++ template class



// Template for GCD
template<typename AType> AType VARGCD(int nargs, ...)

va_list arglist;
va_start(arglist, nargs);

AType *terms = new AType[nargs];
AType result = 0;

// put values into an array
for (int i = 0; i < nargs; i++)

terms[i] = va_arg(arglist, AType);
if (terms[i] < 0)

va_end(arglist);
return (AType)0;


va_end(arglist);

int shift = 0;
int numEven = 0;
int numOdd = 0;
int smallindex = -1;

// Step 1 count number of even and odd
do
n−k / 2.
if (numEven == 0)

for (int i = 0; i < nargs; i++)



while (numEven + numOdd > 1);

// only one remaining element multiply the final answer by 2s at the end.
for (int i = 0; i < nargs; i++)

if (terms[i] == 0)
continue;

return terms[i] << shift;


return 0;
;





share|cite|improve this answer









$endgroup$




















    0












    $begingroup$

    Here's the c++ code to find the gcd (greatest common divisor) of three numbers:



     #include <iostream.h>
    #include<conio.h>
    void main()

    int x,y,z;
    int d,i;
    cout<<"enter three numbers: ";
    cin>>x>>y>>z;
    d=1;
    i=1;

    //condition required

    while(i<=x&&i<=y&&i<=z)

    if(x%i==0&&y%i==0&&z%i==0)

    d=i;
    i++;



    cout<<"GCD: "<<d;
    getch();






    share|cite|improve this answer










    New contributor




    Palomi Kurade is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






    $endgroup$












      Your Answer





      StackExchange.ifUsing("editor", function ()
      return StackExchange.using("mathjaxEditing", function ()
      StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
      StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
      );
      );
      , "mathjax-editing");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "69"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      noCode: true, onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f93731%2ffinding-the-gcd-of-three-numbers%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      9












      $begingroup$

      As others say, one way to do it is using the identity $gcd(a,b,c)=gcd(a,(gcd(b,c))$.
      This identity is true since the "gcd" is the maximal element of the intersection of the sets of factors of the inputs. For example, taking $gcd(6,10)$, the set of factors of $6$ is $6, 3, 2, 1$, the set of factors of $10$ is $10, 5, 2, 1$, their intersection is $2, 1$, and the maximal element is $2$. The identity follows since intersections of sets are commutative and associative.



      However, it's also possible to extend Stein's algorithm to handle more than two inputs. Wikipedia gives 5 steps to Stein's algorithm, and we can change them to the following to compute the gcd of a set $S$ of numbers:



      1. If anything in $S$ is zero, remove it.

      2. If everything in $S$ is even, divide everything in $S$ by 2, and then multiply the final answer by 2 at the end.

      3. If some numbers in $S$ are even and some are odd, divide all the even numbers by 2.

      4. If every number in $S$ is odd, then choose an arbitrary element of $S$ and call it $k$. Replace every other element, say $n$, with $|n-k|/2$.

      5. Repeat steps 1–4 until there is only one remaining element in $S$. After you remember to multiply in the twos that you got from step 2, that's your gcd!





      share|cite|improve this answer











      $endgroup$












      • $begingroup$
        I do believe that this algorithm takes the minimal element of the intersection, doesn't it?
        $endgroup$
        – nmagerko
        Dec 23 '11 at 19:26






      • 1




        $begingroup$
        This answer has a mistake in it! In step 4, one should choose the smallest element, and not an arbitrary one. I have tried to edit the answer twice, both tries were rejected.
        $endgroup$
        – AmiguelS
        Jan 23 '18 at 14:54
















      9












      $begingroup$

      As others say, one way to do it is using the identity $gcd(a,b,c)=gcd(a,(gcd(b,c))$.
      This identity is true since the "gcd" is the maximal element of the intersection of the sets of factors of the inputs. For example, taking $gcd(6,10)$, the set of factors of $6$ is $6, 3, 2, 1$, the set of factors of $10$ is $10, 5, 2, 1$, their intersection is $2, 1$, and the maximal element is $2$. The identity follows since intersections of sets are commutative and associative.



      However, it's also possible to extend Stein's algorithm to handle more than two inputs. Wikipedia gives 5 steps to Stein's algorithm, and we can change them to the following to compute the gcd of a set $S$ of numbers:



      1. If anything in $S$ is zero, remove it.

      2. If everything in $S$ is even, divide everything in $S$ by 2, and then multiply the final answer by 2 at the end.

      3. If some numbers in $S$ are even and some are odd, divide all the even numbers by 2.

      4. If every number in $S$ is odd, then choose an arbitrary element of $S$ and call it $k$. Replace every other element, say $n$, with $|n-k|/2$.

      5. Repeat steps 1–4 until there is only one remaining element in $S$. After you remember to multiply in the twos that you got from step 2, that's your gcd!





      share|cite|improve this answer











      $endgroup$












      • $begingroup$
        I do believe that this algorithm takes the minimal element of the intersection, doesn't it?
        $endgroup$
        – nmagerko
        Dec 23 '11 at 19:26






      • 1




        $begingroup$
        This answer has a mistake in it! In step 4, one should choose the smallest element, and not an arbitrary one. I have tried to edit the answer twice, both tries were rejected.
        $endgroup$
        – AmiguelS
        Jan 23 '18 at 14:54














      9












      9








      9





      $begingroup$

      As others say, one way to do it is using the identity $gcd(a,b,c)=gcd(a,(gcd(b,c))$.
      This identity is true since the "gcd" is the maximal element of the intersection of the sets of factors of the inputs. For example, taking $gcd(6,10)$, the set of factors of $6$ is $6, 3, 2, 1$, the set of factors of $10$ is $10, 5, 2, 1$, their intersection is $2, 1$, and the maximal element is $2$. The identity follows since intersections of sets are commutative and associative.



      However, it's also possible to extend Stein's algorithm to handle more than two inputs. Wikipedia gives 5 steps to Stein's algorithm, and we can change them to the following to compute the gcd of a set $S$ of numbers:



      1. If anything in $S$ is zero, remove it.

      2. If everything in $S$ is even, divide everything in $S$ by 2, and then multiply the final answer by 2 at the end.

      3. If some numbers in $S$ are even and some are odd, divide all the even numbers by 2.

      4. If every number in $S$ is odd, then choose an arbitrary element of $S$ and call it $k$. Replace every other element, say $n$, with $|n-k|/2$.

      5. Repeat steps 1–4 until there is only one remaining element in $S$. After you remember to multiply in the twos that you got from step 2, that's your gcd!





      share|cite|improve this answer











      $endgroup$



      As others say, one way to do it is using the identity $gcd(a,b,c)=gcd(a,(gcd(b,c))$.
      This identity is true since the "gcd" is the maximal element of the intersection of the sets of factors of the inputs. For example, taking $gcd(6,10)$, the set of factors of $6$ is $6, 3, 2, 1$, the set of factors of $10$ is $10, 5, 2, 1$, their intersection is $2, 1$, and the maximal element is $2$. The identity follows since intersections of sets are commutative and associative.



      However, it's also possible to extend Stein's algorithm to handle more than two inputs. Wikipedia gives 5 steps to Stein's algorithm, and we can change them to the following to compute the gcd of a set $S$ of numbers:



      1. If anything in $S$ is zero, remove it.

      2. If everything in $S$ is even, divide everything in $S$ by 2, and then multiply the final answer by 2 at the end.

      3. If some numbers in $S$ are even and some are odd, divide all the even numbers by 2.

      4. If every number in $S$ is odd, then choose an arbitrary element of $S$ and call it $k$. Replace every other element, say $n$, with $|n-k|/2$.

      5. Repeat steps 1–4 until there is only one remaining element in $S$. After you remember to multiply in the twos that you got from step 2, that's your gcd!






      share|cite|improve this answer














      share|cite|improve this answer



      share|cite|improve this answer








      edited Dec 23 '11 at 18:41









      J. M. is not a mathematician

      61.3k5152290




      61.3k5152290










      answered Dec 23 '11 at 18:37









      LopsyLopsy

      3,7731423




      3,7731423











      • $begingroup$
        I do believe that this algorithm takes the minimal element of the intersection, doesn't it?
        $endgroup$
        – nmagerko
        Dec 23 '11 at 19:26






      • 1




        $begingroup$
        This answer has a mistake in it! In step 4, one should choose the smallest element, and not an arbitrary one. I have tried to edit the answer twice, both tries were rejected.
        $endgroup$
        – AmiguelS
        Jan 23 '18 at 14:54

















      • $begingroup$
        I do believe that this algorithm takes the minimal element of the intersection, doesn't it?
        $endgroup$
        – nmagerko
        Dec 23 '11 at 19:26






      • 1




        $begingroup$
        This answer has a mistake in it! In step 4, one should choose the smallest element, and not an arbitrary one. I have tried to edit the answer twice, both tries were rejected.
        $endgroup$
        – AmiguelS
        Jan 23 '18 at 14:54
















      $begingroup$
      I do believe that this algorithm takes the minimal element of the intersection, doesn't it?
      $endgroup$
      – nmagerko
      Dec 23 '11 at 19:26




      $begingroup$
      I do believe that this algorithm takes the minimal element of the intersection, doesn't it?
      $endgroup$
      – nmagerko
      Dec 23 '11 at 19:26




      1




      1




      $begingroup$
      This answer has a mistake in it! In step 4, one should choose the smallest element, and not an arbitrary one. I have tried to edit the answer twice, both tries were rejected.
      $endgroup$
      – AmiguelS
      Jan 23 '18 at 14:54





      $begingroup$
      This answer has a mistake in it! In step 4, one should choose the smallest element, and not an arbitrary one. I have tried to edit the answer twice, both tries were rejected.
      $endgroup$
      – AmiguelS
      Jan 23 '18 at 14:54












      0












      $begingroup$

      Here is J.M. s algorithm implemented as a c++ template class



      // Template for GCD
      template<typename AType> AType VARGCD(int nargs, ...)

      va_list arglist;
      va_start(arglist, nargs);

      AType *terms = new AType[nargs];
      AType result = 0;

      // put values into an array
      for (int i = 0; i < nargs; i++)

      terms[i] = va_arg(arglist, AType);
      if (terms[i] < 0)

      va_end(arglist);
      return (AType)0;


      va_end(arglist);

      int shift = 0;
      int numEven = 0;
      int numOdd = 0;
      int smallindex = -1;

      // Step 1 count number of even and odd
      do
      n−k / 2.
      if (numEven == 0)

      for (int i = 0; i < nargs; i++)



      while (numEven + numOdd > 1);

      // only one remaining element multiply the final answer by 2s at the end.
      for (int i = 0; i < nargs; i++)

      if (terms[i] == 0)
      continue;

      return terms[i] << shift;


      return 0;
      ;





      share|cite|improve this answer









      $endgroup$

















        0












        $begingroup$

        Here is J.M. s algorithm implemented as a c++ template class



        // Template for GCD
        template<typename AType> AType VARGCD(int nargs, ...)

        va_list arglist;
        va_start(arglist, nargs);

        AType *terms = new AType[nargs];
        AType result = 0;

        // put values into an array
        for (int i = 0; i < nargs; i++)

        terms[i] = va_arg(arglist, AType);
        if (terms[i] < 0)

        va_end(arglist);
        return (AType)0;


        va_end(arglist);

        int shift = 0;
        int numEven = 0;
        int numOdd = 0;
        int smallindex = -1;

        // Step 1 count number of even and odd
        do
        n−k / 2.
        if (numEven == 0)

        for (int i = 0; i < nargs; i++)



        while (numEven + numOdd > 1);

        // only one remaining element multiply the final answer by 2s at the end.
        for (int i = 0; i < nargs; i++)

        if (terms[i] == 0)
        continue;

        return terms[i] << shift;


        return 0;
        ;





        share|cite|improve this answer









        $endgroup$















          0












          0








          0





          $begingroup$

          Here is J.M. s algorithm implemented as a c++ template class



          // Template for GCD
          template<typename AType> AType VARGCD(int nargs, ...)

          va_list arglist;
          va_start(arglist, nargs);

          AType *terms = new AType[nargs];
          AType result = 0;

          // put values into an array
          for (int i = 0; i < nargs; i++)

          terms[i] = va_arg(arglist, AType);
          if (terms[i] < 0)

          va_end(arglist);
          return (AType)0;


          va_end(arglist);

          int shift = 0;
          int numEven = 0;
          int numOdd = 0;
          int smallindex = -1;

          // Step 1 count number of even and odd
          do
          n−k / 2.
          if (numEven == 0)

          for (int i = 0; i < nargs; i++)



          while (numEven + numOdd > 1);

          // only one remaining element multiply the final answer by 2s at the end.
          for (int i = 0; i < nargs; i++)

          if (terms[i] == 0)
          continue;

          return terms[i] << shift;


          return 0;
          ;





          share|cite|improve this answer









          $endgroup$



          Here is J.M. s algorithm implemented as a c++ template class



          // Template for GCD
          template<typename AType> AType VARGCD(int nargs, ...)

          va_list arglist;
          va_start(arglist, nargs);

          AType *terms = new AType[nargs];
          AType result = 0;

          // put values into an array
          for (int i = 0; i < nargs; i++)

          terms[i] = va_arg(arglist, AType);
          if (terms[i] < 0)

          va_end(arglist);
          return (AType)0;


          va_end(arglist);

          int shift = 0;
          int numEven = 0;
          int numOdd = 0;
          int smallindex = -1;

          // Step 1 count number of even and odd
          do
          n−k / 2.
          if (numEven == 0)

          for (int i = 0; i < nargs; i++)



          while (numEven + numOdd > 1);

          // only one remaining element multiply the final answer by 2s at the end.
          for (int i = 0; i < nargs; i++)

          if (terms[i] == 0)
          continue;

          return terms[i] << shift;


          return 0;
          ;






          share|cite|improve this answer












          share|cite|improve this answer



          share|cite|improve this answer










          answered Apr 21 '16 at 1:02









          Paul BaxterPaul Baxter

          101




          101





















              0












              $begingroup$

              Here's the c++ code to find the gcd (greatest common divisor) of three numbers:



               #include <iostream.h>
              #include<conio.h>
              void main()

              int x,y,z;
              int d,i;
              cout<<"enter three numbers: ";
              cin>>x>>y>>z;
              d=1;
              i=1;

              //condition required

              while(i<=x&&i<=y&&i<=z)

              if(x%i==0&&y%i==0&&z%i==0)

              d=i;
              i++;



              cout<<"GCD: "<<d;
              getch();






              share|cite|improve this answer










              New contributor




              Palomi Kurade is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.






              $endgroup$

















                0












                $begingroup$

                Here's the c++ code to find the gcd (greatest common divisor) of three numbers:



                 #include <iostream.h>
                #include<conio.h>
                void main()

                int x,y,z;
                int d,i;
                cout<<"enter three numbers: ";
                cin>>x>>y>>z;
                d=1;
                i=1;

                //condition required

                while(i<=x&&i<=y&&i<=z)

                if(x%i==0&&y%i==0&&z%i==0)

                d=i;
                i++;



                cout<<"GCD: "<<d;
                getch();






                share|cite|improve this answer










                New contributor




                Palomi Kurade is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.






                $endgroup$















                  0












                  0








                  0





                  $begingroup$

                  Here's the c++ code to find the gcd (greatest common divisor) of three numbers:



                   #include <iostream.h>
                  #include<conio.h>
                  void main()

                  int x,y,z;
                  int d,i;
                  cout<<"enter three numbers: ";
                  cin>>x>>y>>z;
                  d=1;
                  i=1;

                  //condition required

                  while(i<=x&&i<=y&&i<=z)

                  if(x%i==0&&y%i==0&&z%i==0)

                  d=i;
                  i++;



                  cout<<"GCD: "<<d;
                  getch();






                  share|cite|improve this answer










                  New contributor




                  Palomi Kurade is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.






                  $endgroup$



                  Here's the c++ code to find the gcd (greatest common divisor) of three numbers:



                   #include <iostream.h>
                  #include<conio.h>
                  void main()

                  int x,y,z;
                  int d,i;
                  cout<<"enter three numbers: ";
                  cin>>x>>y>>z;
                  d=1;
                  i=1;

                  //condition required

                  while(i<=x&&i<=y&&i<=z)

                  if(x%i==0&&y%i==0&&z%i==0)

                  d=i;
                  i++;



                  cout<<"GCD: "<<d;
                  getch();







                  share|cite|improve this answer










                  New contributor




                  Palomi Kurade is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  share|cite|improve this answer



                  share|cite|improve this answer








                  edited 21 hours ago





















                  New contributor




                  Palomi Kurade is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  answered yesterday









                  Palomi KuradePalomi Kurade

                  111




                  111




                  New contributor




                  Palomi Kurade is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.





                  New contributor





                  Palomi Kurade is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.






                  Palomi Kurade is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Mathematics Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      Use MathJax to format equations. MathJax reference.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f93731%2ffinding-the-gcd-of-three-numbers%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      How should I support this large drywall patch? Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?How do I cover large gaps in drywall?How do I keep drywall around a patch from crumbling?Can I glue a second layer of drywall?How to patch long strip on drywall?Large drywall patch: how to avoid bulging seams?Drywall Mesh Patch vs. Bulge? To remove or not to remove?How to fix this drywall job?Prep drywall before backsplashWhat's the best way to fix this horrible drywall patch job?Drywall patching using 3M Patch Plus Primer

                      random experiment with two different functions on unit interval Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern)Random variable and probability space notionsRandom Walk with EdgesFinding functions where the increase over a random interval is Poisson distributedNumber of days until dayCan an observed event in fact be of zero probability?Unit random processmodels of coins and uniform distributionHow to get the number of successes given $n$ trials , probability $P$ and a random variable $X$Absorbing Markov chain in a computer. Is “almost every” turned into always convergence in computer executions?Stopped random walk is not uniformly integrable

                      Lowndes Grove History Architecture References Navigation menu32°48′6″N 79°57′58″W / 32.80167°N 79.96611°W / 32.80167; -79.9661132°48′6″N 79°57′58″W / 32.80167°N 79.96611°W / 32.80167; -79.9661178002500"National Register Information System"Historic houses of South Carolina"Lowndes Grove""+32° 48' 6.00", −79° 57' 58.00""Lowndes Grove, Charleston County (260 St. Margaret St., Charleston)""Lowndes Grove"The Charleston ExpositionIt Happened in South Carolina"Lowndes Grove (House), Saint Margaret Street & Sixth Avenue, Charleston, Charleston County, SC(Photographs)"Plantations of the Carolina Low Countrye