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
$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.
elementary-number-theory algorithms
$endgroup$
|
show 3 more comments
$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.
elementary-number-theory algorithms
$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
|
show 3 more comments
$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.
elementary-number-theory algorithms
$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
elementary-number-theory algorithms
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
|
show 3 more comments
$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
|
show 3 more comments
3 Answers
3
active
oldest
votes
$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:
- If anything in $S$ is zero, remove it.
- If everything in $S$ is even, divide everything in $S$ by 2, and then multiply the final answer by 2 at the end.
- If some numbers in $S$ are even and some are odd, divide all the even numbers by 2.
- 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$.
- 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!
$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
add a comment |
$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;
;
$endgroup$
add a comment |
$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();
New contributor
$endgroup$
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
$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:
- If anything in $S$ is zero, remove it.
- If everything in $S$ is even, divide everything in $S$ by 2, and then multiply the final answer by 2 at the end.
- If some numbers in $S$ are even and some are odd, divide all the even numbers by 2.
- 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$.
- 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!
$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
add a comment |
$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:
- If anything in $S$ is zero, remove it.
- If everything in $S$ is even, divide everything in $S$ by 2, and then multiply the final answer by 2 at the end.
- If some numbers in $S$ are even and some are odd, divide all the even numbers by 2.
- 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$.
- 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!
$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
add a comment |
$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:
- If anything in $S$ is zero, remove it.
- If everything in $S$ is even, divide everything in $S$ by 2, and then multiply the final answer by 2 at the end.
- If some numbers in $S$ are even and some are odd, divide all the even numbers by 2.
- 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$.
- 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!
$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:
- If anything in $S$ is zero, remove it.
- If everything in $S$ is even, divide everything in $S$ by 2, and then multiply the final answer by 2 at the end.
- If some numbers in $S$ are even and some are odd, divide all the even numbers by 2.
- 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$.
- 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!
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
add a comment |
$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
add a comment |
$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;
;
$endgroup$
add a comment |
$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;
;
$endgroup$
add a comment |
$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;
;
$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;
;
answered Apr 21 '16 at 1:02
Paul BaxterPaul Baxter
101
101
add a comment |
add a comment |
$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();
New contributor
$endgroup$
add a comment |
$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();
New contributor
$endgroup$
add a comment |
$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();
New contributor
$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();
New contributor
edited 21 hours ago
New contributor
answered yesterday
Palomi KuradePalomi Kurade
111
111
New contributor
New contributor
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
$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