java 如何在单个方法中找到三个数字的 GCD
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13573026/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find the GCD of three numbers within a single method
提问by tech_geek23
I've got to ensure that the GCD between 3 numbers is no greater than 1.
我必须确保 3 个数字之间的 GCD 不大于 1。
Here's the code I have so far for the method:
这是我迄今为止为该方法编写的代码:
private int greatestCommonFactor(int a, int b, int c)
{
for(int n = 0; n <= number; n++)
{
if()
}
return 1;
}
the return 1
was already there when I started working on the lab. How can I make sure that the GCD is no more than 1? And return all three integers?
return 1
当我开始在实验室工作时,它已经在那里了。如何确保 GCD 不超过 1?并返回所有三个整数?
Here's the remainder of the code if it helps in figuring out what needs to be done:
下面是代码的其余部分,如果它有助于弄清楚需要做什么:
import static java.lang.System.*;
public class Triples
{
private int number;
public Triples()
{
this(0);
}
public Triples(int num)
{
number = num;
}
public void setNum(int num)
{
number = num;
}
private int greatestCommonFactor(int a, int b, int c)
{
for(int n = 0; n <= number; n++)
{
if()
}
return 1;
}
public String toString()
{
String output="";
int max = number;
for(a = 1; a <= max; a++)
{
for(b = a +1; b <= max; b++)
{
for(c = b + 1; c <= max; c++)
{
if(Math.pow(a, 2)+ Math.pow(b, 2)== Math.pow(c, 2))
{
if((a%2==1 && b%2==0)|| (a%2==0 && b%2==1))
}
}
}
}
return output+"\n";
}
}
UPDATE
更新
Here is my new coding for the same lab:
这是我为同一个实验室编写的新代码:
import static java.lang.System.*;
public class Triples
{
private int number;
public Triples()
{
this(0);
}
public Triples(int num)
{
number = num;
}
public void setNum(int num)
{
number = num;
}
private int greatestCommonFactor(int a, int b, int c)
{
for(int n = 0; n <= number; n++)
{
int max = number;
for(a = 1; a <= max; a++)
{
a = n;
for(b = a +1; b <= max; b++)
{
b =n;
for(c = b + 1; c <= max; c++)
{
c = n;
if(Math.pow(a, 2)+ Math.pow(b, 2)== Math.pow(c, 2))
{
if((a%2==1 && b%2==0)|| (a%2==0 && b%2==1))
{
if(a%2<=1 && b%2<=1 && c%2<=1)
{
return 1;
}
}
}
}
}
}
}
return 1;
}
public String toString()
{
String output="";
output = greatestCommonFactor(a, b, c);
return output+"\n";
}
}
回答by Ted Hopp
You can use Euclid's algorithmto calculate the GCD of a
and b
. Call the result d
. Then the GCD of a
, b
, and c
is the GCD of c
and d
; for that, you can use Euclid's algorithm again.
你可以用欧几里德的算法来计算的GCDa
和b
。调用结果d
。然后的GCD a
,b
和c
是的GCDc
和d
; 为此,您可以再次使用 Euclid 算法。
回答by sampson-chen
Here's a brute-force way if you don't care about efficiency:
如果您不关心效率,这是一种蛮力方法:
private int greatestCommonFactor(int a, int b, int c)
{
limit = Math.min(a, b);
limit = Math.min(limit, c);
for(int n = limit; n >= 2; n--)
{
if ( (a % n == 0) && (b % n == 0) && (c % n == 0) ) {
return n;
}
}
return 1;
}
Explanation:
解释:
- You can save some work by only checking up to the minimum of
(a, b, c)
. Any number greater than that definitely won't be a GCD of all 3. - You need to start your loop at
n = limit
instead ofn = 0
and count backwards. - As soon as we come across a number that produces zero remainder for
(a, b, c)
, that must be the GCD. - If nothing is found within the loop, GCD defaults to 1.
- 您可以通过最多只检查
(a, b, c)
.任何大于这个的数字肯定不会是所有 3 的 GCD。 - 您需要在
n = limit
而不是开始循环n = 0
并倒数。 - 一旦我们遇到一个对 产生零余数的数字
(a, b, c)
,那一定是 GCD。 - 如果在循环中没有找到任何东西,GCD 默认为 1。