對一組數字計算最大公約數和最小公倍數,最簡單的方法是什麼?什麼數學函數可以用來找到這些信息?如何在一組數字上找到GCD,LCM
回答
我用Euclid's algorithm找到最大的普通di兩個數字的遮陽板;它可以迭代以獲得更大數字集合的GCD。
private static long gcd(long a, long b)
{
while (b > 0)
{
long temp = b;
b = a % b; // % is remainder
a = temp;
}
return a;
}
private static long gcd(long[] input)
{
long result = input[0];
for(int i = 1; i < input.length; i++) result = gcd(result, input[i]);
return result;
}
最小公倍數是有點麻煩,但可能是最好的辦法是reduction by the GCD,可同樣迭代:
private static long lcm(long a, long b)
{
return a * (b/gcd(a, b));
}
private static long lcm(long[] input)
{
long result = input[0];
for(int i = 1; i < input.length; i++) result = lcm(result, input[i]);
return result;
}
它沒有構建它的功能。您可以使用Euclid's algorithm找到兩個數字的GCD。
對於一組數
的GCD(a_1,a_2,a_3,...,a_n) = GCD(GCD(a_1, a_2), a_3, a_4,..., a_n)
應用它遞歸。
同樣爲LCM:
LCM(a,b) = a * b/GCD(a,b)
LCM(a_1,a_2,a_3,...,a_n) = LCM(LCM(a_1, a_2), a_3, a_4,..., a_n)
BigInteger類有一個gcd方法。 +1來回答一組整數的問題,而不僅僅是一對。 – 2010-11-18 01:21:18
沒有爲GCD一個Euclid's algorithm,
public int GCF(int a, int b) {
if (b == 0) return a;
else return (GCF (b, a % b));
}
順便提一下,a
和b
應該大於或等於0
,和LCM = |ab|/GCF(a, b)
[Java不會優化尾部調用。](http://www.drdobbs.com/jvm/tail-call-optimization-and-java/240167044) – 2015-05-07 02:17:31
int lcmcal(int i,int y)
{
int n,x,s=1,t=1;
for(n=1;;n++)
{
s=i*n;
for(x=1;t<s;x++)
{
t=y*x;
}
if(s==t)
break;
}
return(s);
}
int lcm(int x,int y){
int i=1;
while(true){
if(!(x*i)%y)
return x*i;
i++;
}
int gcf(int a, int b)
{
while (a != b) // while the two numbers are not equal...
{
// ...subtract the smaller one from the larger one
if (a > b) a -= b; // if a is larger than b, subtract b from a
else b -= a; // if b is larger than a, subtract a from b
}
return a; // or return b, a will be equal to b either way
}
int lcm(int a, int b)
{
// the lcm is simply (a * b) divided by the gcf of the two
return (a * b)/gcf(a, b);
}
一些解釋會很好... – 2014-08-17 09:39:33
這是歐幾里德算法的實現。 要找到兩個數字的GCF,從較小的數字中減去較大的數字,而兩個數字不相等。 例如: 查找6和10的GCF: 10 6 // 10大於6,所以從10中減去6; 4 6 // 6現在大於4,所以從10中減去4; 4 2 // 4大於2,所以從4減去2; 2 2 //這兩個數字現在相等,這就是GCF; 數學中的LCM只是第二個數字除以GCF的第一個數字。我的意思是在回答中有 – user3026735 2014-08-17 13:11:15
。並非所有人都閱讀評論 – 2014-08-17 15:14:24
更容易理解:
{ int num1,num2;
int m1,m2,r;
cout<<"Enter two numbers: "<<endl;
cin>>num1>>num2;
if(num1>num2)
{
m1=num1;
m2=num2;
}
else
{
m1=num2;
m2=num1;
}
while(r!=0)
{ r=m1%m2; m1=m2; m2=r;
}
cout<<"result of gcd of two number is "<<m1<<endl;
int lcm= (num1*num2)/m1;
cout<<"result of lcm of two number is "<<lcm;
}
- 查看更多的部位:http://www.easycppcodes.com/2015/01/find-gcd-and-lcm-of-two-numbers-using.html#sthash.lAWwqgS5.dpuf
import java.util.Scanner; 公共類Lcmhcf {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner scan = new Scanner(System.in);
int n1,n2,x,y,lcm,hcf;
System.out.println("Enter any 2 numbers....");
n1=scan.nextInt();
n2=scan.nextInt();
x=n1;
y=n2;
do{
if(n1>n2){
n1=n1-n2;
}
else{
n2=n2-n1;
}
} while(n1!=n2);
hcf=n1;
lcm=x*y/hcf;
System.out.println("HCF IS = "+hcf);
System.out.println("LCM IS = "+lcm);
}
}
//## Heading ##By Rajeev Lochan Sen
歡迎來到SO。請仔細檢查您的代碼的格式。 – 2016-04-15 12:25:36
在Java 8中,有更優雅和功能的方式來解決這個問題。
LCM:
private static int lcm(int numberOne, int numberTwo) {
final int bigger = Math.max(numberOne, numberTwo);
final int smaller = Math.min(numberOne, numberTwo);
return IntStream.rangeClosed(1,smaller)
.filter(factor -> (factor * bigger) % smaller == 0)
.map(factor -> Math.abs(factor * bigger))
.findFirst()
.getAsInt();
}
GCD:
private static int gcd(int numberOne, int numberTwo) {
return (numberTwo == 0) ? numberOne : gcd(numberTwo, numberOne % numberTwo);
}
當然,如果一個參數爲0,這兩種方法是行不通的。
如果你可以使用Java 8(實際上是希望),你可以使用lambda表達式在功能上解決這個問題:
private static int gcd(int x, int y) {
return (y == 0) ? x : gcd(y, x % y);
}
public static int gcd(int... numbers) {
return Arrays.stream(numbers).reduce(0, (x, y) -> gcd(x, y));
}
public static int lcm(int... numbers) {
return Arrays.stream(numbers).reduce(1, (x, y) -> x * (y/gcd(x, y)));
}
我面向自己的Jeffrey Hantin's answer,但
- 計算GCD功能
- 使用varargs-Syntax獲得更簡單的API(我不確定過載是否能正常工作,但它確實在我的機器上)
- 轉化的
numbers
- 陣列的GCD到功能語法,這是更緊湊,更容易IMO閱讀(至少如果你使用函數式編程)
這種方法可能是稍微慢一些,由於額外的函數調用,但對於大多數用例來說,這可能根本就不重要。
很乾淨很喜歡它 – Spets 2016-12-12 06:35:55
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n0 = input.nextInt(); // number of intended input.
int [] MyList = new int [n0];
for (int i = 0; i < n0; i++)
MyList[i] = input.nextInt();
//input values stored in an array
int i = 0;
int count = 0;
int gcd = 1; // Initial gcd is 1
int k = 2; // Possible gcd
while (k <= MyList[i] && k <= MyList[i]) {
if (MyList[i] % k == 0 && MyList[i] % k == 0)
gcd = k; // Update gcd
k++;
count++; //checking array for gcd
}
// int i = 0;
MyList [i] = gcd;
for (int e: MyList) {
System.out.println(e);
}
}
}
嗨,歡迎來到Stack Overflow。感謝您發佈這個問題的答案,但只包含代碼的答案通常對人們沒有幫助。查看接受的答案,獲得一個好答案的例子。請參閱[我如何寫出一個好的答案?](http://stackoverflow.com/help/how-to-answer)瞭解更多信息。 – Adrian 2016-11-24 04:29:56
int main()
{
int n1,n2,num1,num2,rem,gcd,lcm;
printf("Enter a number:");
scanf("%d %d",&num1,&num2);
num1=n1;
num2=n2;
while(num2!=0)
{
rem=num1%num2;
num1=num2;
num2=rem;
}
gcd=num1;
lcm=(n1*n2)/gcd;
printf("Gcd=%d\n",gcd);
printf("Lcm=%d\n",lcm);
}
我不確定這是Java。 – Manur 2017-03-24 16:11:50
//找到兩個號碼[非常簡單的方法]
類LCM
{
static void main(int n1,int n2)
{
int lcm;
int a=n1;
int b=n2;
System.out.println("\f");
if(n1==0||n2==0)
{
System.out.println("enter different number");
}
else
{
while(a!=b)
{
if(a>b)
{
a=a-b;
}
else
{
b=b-a;
}
}
int GCD=a;
lcm=(n1*n2)/GCD;
System.out.println("the LCM is "+lcm);
}
}}
那就是LCM一個非常簡單的方法OD ... [嘗試自己理解的Cuz編碼方式理解... :)]
爲gcd
你CAD如下操作:
String[] ss = new Scanner(System.in).nextLine().split("\\s+");
BigInteger bi,bi2 = null;
bi2 = new BigInteger(ss[1]);
for(int i = 0 ; i<ss.length-1 ; i+=2)
{
bi = new BigInteger(ss[i]);
bi2 = bi.gcd(bi2);
}
System.out.println(bi2.toString());
基本上找到最大公約數和最小公倍數的一組數字可以用下面的公式,
LCM(a, b) X HCF(a, b) = a * b
與此同時,在Java中,你可以使用Euclid算法找到最大公約數和最小公倍數,這樣
public static int GCF(int a, int b)
{
if (b == 0)
{
return a;
}
else
{
return (GCF(b, a % b));
}
}
你可以參考this資源找到關於歐幾里得算法的例子。
- 1. 如何有效地獲得一系列數字的GCD和LCM?
- 2. 計算5個數字的gcd和lcm
- 3. GCD和LCM關係
- 4. LCM和GCD 3編號 - Python
- 5. 找到兩個數字的LCM
- 6. 尋找一系列數字的LCM
- 7. 如何找到兩個數字的最小公倍數(LCM)
- 8. GCD/LCM x86 Intel NASM彙編程序中的LCM計算錯誤
- 9. LCM和GCD不能正常工作
- 10. 找到n個數字的GCD
- 11. 如何在數組中找到數字?
- 12. 使用C中的數組尋找GCD
- 13. While循環在GCD中不起作用,LCM
- 14. 試圖找到使用php函數返回數組的gcd?
- 15. python程序找到hcf和lcm
- 16. 在二維字符數組上找到一個詞
- 17. 如何找到一個PHP數組
- 18. 找到n個數字的gcd最快的方法是什麼?
- 19. 如何找到一組特定的字母的字母在PHP
- 20. 如何在字符串中找到一組字符
- 21. 貓鼬在字段組合上找到
- 22. 編譯器在Windows上找不到GCD頭文件
- 23. 如何在數組中找到最大和最小數字c
- 24. 如何在多維數組中找到匹配的數字?
- 25. N + 1個連續數字的LCM
- 26. 如何在數組中找到峯值?
- 27. 在字符串中找到一個數組中的字符串
- 28. 在字符數組中找到一個特定的字符
- 29. 如何在Python中的字符串中找到一個數字?
- 30. 如何編組一個C#字符串數組到VB6數組?
GCD的另一個完美解決方案是在http:// stackoverflow。 com/a/4009247/218198 – milkersarac 2014-03-13 14:56:05
複雜性方面有更快的方法嗎? – CinCout 2015-01-02 11:35:23
@binaryBaBa,比整數數量中的線性更快是不可能的,因爲你不得不跳過檢查一些。至於GCD本身,[二進制GCD](https://en.wikipedia.org/wiki/Binary_GCD_algorithm)對於任意精度數可能會更快,因爲它轉移而不是分割。 – 2015-01-05 03:27:10