Finding GCD of Array Code C language

46,424

Solution 1

#include <stdio.h>

unsigned gcd(unsigned x, unsigned y){
    unsigned wk;
    if(x<y){ wk=x;x=y;y=wk; }
    while(y){
        wk = x%y;
        x=y;
        y=wk;
    }
    return x;
}

int gcd_a(int n, int a[n]){
    if(n==1) return a[0];
    if(n==2) return gcd(a[0], a[1]);
    int h = n / 2;
    return gcd(gcd_a(h, &a[0]), gcd_a(n - h, &a[h]));
}

int main(void){
    int A[10]={112, 160, 180, 240, 288, 32, 480, 96, 60, 72};
    int size_A = sizeof(A)/sizeof(*A);
    int gcd = gcd_a(size_A, A);
    printf("%d\n", gcd);
    return 0;
}

Solution 2

#include <stdio.h>
#include <stdlib.h>

int gcd(int a, int b)
{
    int r;
    while(a)
    {
        r=b%a;
        b=a;
        a=r;
    }
    return b;
}
int gcd_(int* A, int N)
{
    int c=gcd(*A,*(A+1));
    int i,g;
    for(i=1;i<N-1;i++)
    {
        g=gcd(c,*(A+1+i));
        c=g;
    }
    return c;
}
int main(void)
{
    int A[]={96,60,32,72,84,90};
    int N=sizeof(A)/sizeof(A[0]);
    int t=gcd_(A,N);
    printf("%d",t);
    return 0;
}

Solution 3

#include <stdio.h>

static int gcd(int x, int y)
{
    int r;

    if (x <= 0 || y <= 0)
        return(0);

    while ((r = x % y) != 0)
    {
        x = y;
        y = r;
    }
    return(y);
}

int main(void)
{
    int A[10] = { 112, 160, 180, 240, 288, 32, 480, 96, 60, 72 };
    int g = A[0];

    for (int i = 1; i < 10; i++)
        g = gcd(g, A[i]);

    printf("The Greatest Common Denominator is: %d\n", g);

    return 0;
}

The answer is 4. This is clearly correct; it is the GCD of 32 and 60; everything else is divisible by 4.

If desired, you could optimize the loop with:

    for (int i = 1; i < 10 && g != 1; i++)
        g = gcd(g, A[i]);

When the GCD is 1, it won't get any bigger,

Share:
46,424
Cod_Ubau
Author by

Cod_Ubau

Updated on October 08, 2020

Comments

  • Cod_Ubau
    Cod_Ubau over 3 years

    I am trying to write a program in C. The program is supposed to find the GCD (greatest common divisor) of a given array. I am trying to use the smallest number of the array to find the GCD. I was wondering whats wrong with my last loop. I havent figured a way on how to check if the division is giving any decimal points in order to stop the loop. This is my code

    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int A[10]={112, 160, 180, 240, 288, 32, 480, 96, 60, 72};
        int i;
        int j;
        int minimum = A[0];
        int GCD;
        int temp;
    
        for (i=1;i<9;i++)
        {
            if( A[i] < minimum)
            {
                minimum = A[i];
            }
        }
    
        for (i=1; i < minimum/2; i++)
        {
            for (j = 0; j < 9;j++)
            {
                GCD = 2*i;
                temp = ((A[j])/(GCD));
                int check = temp%1;
                if (check == 0)
                    break;
            }
        }
    
        printf("The Greates Common Denominator is: %d", GCD);
    
        return 0;
    }