1

Here I am trying to find lowest common multiple of an array of numbers. I used the following formula to find the value which uses greatest common divisor to find out LCM.

enter image description here

My program calculates GCD correctly, but when it comes to find out LCM using GCD it gives wrong LCM value. What might be wrong in my logic. Any help would be much appreciated.

#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);
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
AL-zami
  • 8,902
  • 15
  • 71
  • 130

4 Answers4

0

Does this help? Or was your aim to calculate GCD and LCM while calling findGCD as few times as possible?

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);
}
Marek Klein
  • 1,410
  • 12
  • 20
0

The above formula mentioned by you is true only for two numbers not multiple numbers(in your case 10). The correct formula say for 3 numbers is:

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

For more info refer this https://math.stackexchange.com/questions/319297/gcd-to-lcm-of-multiple-numbers

Community
  • 1
  • 1
Ishpreet
  • 5,230
  • 2
  • 19
  • 35
0

There are multiple problems in your code:

  • you compute the LCM for all elements of the array, but the GCD only for the 2 initial values.
  • multiplication formula might overflow before you divide by the GCD. You should perform the operation in the opposite order and still check for potential overflow.

  • the prototype for findGCD is incorrect: it returns an int.

Here is a corrected version:

#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;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
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);
}