找到最低公倍数

时间:2016-10-24 11:27:53

标签: c arrays greatest-common-divisor lcm

这里我试图找到一组数字的最低公倍数。我使用以下公式来找到使用最大公约数的值来找出LCM。

enter image description here

我的程序正确计算GCD,但是当使用GCD找出LCM时,它会给出错误的LCM值。我的逻辑可能有什么问题。任何帮助将不胜感激。

#include <stdio.h>

int main() {
   int arr[10] = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };
   int GCD = findGCD(arr[0], arr[1]);
   int LCM = (arr[0] * arr[1]) / GCD;
   int i;
   for (i = 2; i < sizeof(arr) / sizeof(arr[0]); i++) {
        int temp = GCD;
        GCD = findGCD(temp, arr[i]);
        LCM = (temp * arr[i]) / GCD;
   }
   printf("GCD IS %d AND LCM IS %d", GCD, LCM);
}

int findGCD(int num1, int num2) {
    if (num2 == 0) {
        return num1;
    }
    if (num1 % num2 == 0) {
        return num2;
    }
    return findGCD(num2, num1 % num2);
}

4 个答案:

答案 0 :(得分:0)

这有帮助吗?或者你的目标是在尽可能少地调用findGCD的同时计算GCD和LCM?

int main(){
   int arr[10]={10,20,30,40,50,60,70,80,90,100};
   int GCD=arr[0];
   int LCM=arr[0];
   int i;

   for(i=1;i<sizeof(arr)/sizeof(arr[0]);i++){
        GCD = findGCD(GCD,arr[i]);
        LCM = (LCM * arr[i]) / findGCD(LCM, arr[i]);
   }

   printf("GCD IS %d AND LCM IS %d",GCD,LCM);
}

答案 1 :(得分:0)

您提到的上述公式仅适用于两个数字而非多个数字(在您的情况下为10)。 3个数字的正确公式是:

lcm(a,b,c)=abc/gcd(ab,bc,ca)

有关详细信息,请参阅此https://math.stackexchange.com/questions/319297/gcd-to-lcm-of-multiple-numbers

答案 2 :(得分:0)

您的代码中存在多个问题:

  • 您为数组的所有元素计算LCM,但仅对2个初始值计算GCD。
  • 乘法公式可能会在您除以GCD之前溢出。您应该以相反的顺序执行操作,并仍然检查是否存在潜在的溢出。

  • findGCD的原型不正确:它返回了int

这是更正的版本:

#include <limits.h>
#include <stdio.h>

int findGCD(int num1, int num2) {
    if (num2 == 0) {
        return num1;
    }
    if (num1 % num2 == 0) {
        return num2;
    }
    return findGCD(num2, num1 % num2);
}

int main() {
    int arr[10] = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };
    int GCD = arr[0];
    int LCM = arr[0];
    size_t i;

    for (i = 1; i < sizeof(arr) / sizeof(arr[0]); i++) {
        if (LCM == 0 || arr[i] == 0) {
            LCM = 0;
            break;
        }
        GCD = findGCD(GCD, arr[i]);
        LCM = LCM / findGCD(LCM, arr[i]);
        if (arr[i] > INT_MAX / LCM) {
            printf("integer overflow: the LCM exceeds the range of type int\n");
            return 1;
        }
        LCM = LCM * arr[i];
    }
    printf("GCD IS %d AND LCM IS %d", GCD, LCM);
    return 0;
}

答案 3 :(得分:0)

#include <stdio.h>
#include <stdlib.h>
#define VSTUP "cisla.txt"
#define VYSTUP "vystup.txt"

int nsd(int x,int y){
    int delitel = (x < y) ? x : y;
    while(x % delitel != 0 || y % delitel != 0)
        delitel--;
    return delitel;
}

int nsn(int x,int y){
    int nasobek = (x > y) ? x : y;
    while(nasobek % x != 0 || nasobek % y != 0)
        nasobek += (x > y) ? x : y;
    return nasobek;
}

int main(int argc, char** argv) {
    FILE * vstup;
    FILE * vystup;
    int c1, c2;
    int i = 1, j = 1;
    vstup = fopen (VSTUP,"r");
    if (vstup == NULL){
        printf("Soubor %s nebyl otevren.\n",VSTUP);
        return (EXIT_FAILURE);
    }
    vystup = fopen (VYSTUP,"w");
    printf("Vypis cisel ze souboru %s\n-------------------------------- \n",VSTUP);
    fprintf(vystup,"Vypis delitelnych cisel ze souboru %s\n--------------------------------------------\n",VYSTUP);
    printf("%7s%7s%7s%7s%7s\n","poradi","cislo1","cislo2","nsn","nsd");
    fprintf(vystup,"%7s%7s%7s%7s%7s\n","poradi","cislo1","cislo2","nsn","nsd");
    while (fscanf(vstup,"%d %d",&c1,&c2) == 2){
        printf("%6d.%7d%7d%7d%7d\n",i,c1,c2,nsn(c1,c2),nsd(c1,c2));
        if (nsd(c1,c2) != 1){
            fprintf(vystup,"%6d.%7d%7d%7d%7d\n",j,c1,c2,nsn(c1,c2),nsd(c1,c2));
            j++;
        }
        i++;
    }
    printf("\nSoubor %s obsahuje %d dvojic cisel.\n\n",VSTUP,i-1);
    fprintf(vystup,"\nSoubor %s obsahuje %d dvojic cisel.\n",VYSTUP,j-1);
    if (fclose (vstup) == EOF)
        printf("Soubor %s nebyl uzavren.\n",VSTUP);
    if (fclose (vystup) == EOF)
        printf("Soubor %s se nepovedlo vytvorit.\n",VYSTUP);
    else
        printf("Byl vytvoren soubor delitelnych cisel %s.\n\n",VYSTUP);
    return (EXIT_SUCCESS);
}

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define VSTUP "cisla.txt"
#define VYSTUP "vystup.txt"


int mocnina (int z, int e){
    int v = 1;
    for(;e > 0;e--)
        v *= z;
    return v;
}

int prvocislo(int n){
    int i;
    for(i = 2; i <= sqrt(n); ++i) {
        if (n % i == 0) 
            return 0;
        }
    return 1;
}

int main(int argc, char** argv) {
    FILE * vstup;
    FILE * vystup;
    int z,e;
    int i = 1, j = 1;
    vstup = fopen (VSTUP,"r");
    if (vstup == NULL){
        printf("Soubor %s nebyl otevren.\n",VSTUP);
        return (EXIT_FAILURE);
    }
    vystup = fopen (VYSTUP,"w");
    printf("Vystup cisel ze souboru %s\n",VSTUP);
    printf("---------------------------------\n");
    printf("%6s%9s%9s%9s\n","poradi","zaklad","exponent","mocnina");
    fprintf(vystup,"Vystup cisel s prvociselnym zakladem ze souboru %s\n",VYSTUP);
    fprintf(vystup,"---------------------------------------------------------\n");
    while( fscanf (vstup,"%d %d",&z,&e) == 2){
        printf("%5d.%9d%9d%9d\n",i,z,e,mocnina(z,e));
        if (prvocislo(z)){
            fprintf(vystup,"%5d.%8d%8d%8d\n",j,z,e,mocnina(z,e));
            j++;
        }
        i++;
    } 
    fprintf(vystup,"Soubor %s obsahuje %d dvojic cisel.\n",VYSTUP,j-1);
    if (fclose (vstup) == EOF)
        printf("Soubor %s nebyl uzavren.\n",VSTUP);
    if (fclose (vystup) == EOF)
        printf("Soubor %s se nepovedlo vytvorit.\n\n",VYSTUP);
    else
        printf("\nByl vytvoren soubor cisel %s s poctem dvojic cisel rovnym %d.\n\n",VYSTUP,j-1);
    return (EXIT_SUCCESS);
}