summaryrefslogtreecommitdiff
path: root/gcd.c
blob: b0109c87b21be231caa674570ed945db348d24e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <stdlib.h>
#include <stdio.h>

static unsigned long int gcd2(unsigned long int a, unsigned long int b)
{
    unsigned long int c;
    while (b != 0) {
        c = b;
        b = a % b;
        a = c;
    }
    return a;
}

static unsigned long int gcdn(unsigned long int *a, size_t n)
{
    unsigned long int r;
    size_t i;
    r = a[0];
    for(i = 1; i < n; i++) {
        r = gcd2(r, a[i]);
    }
    return r;
}


int main (int argc, char *argv[])
{
    unsigned long int *a;
    size_t i, n;

    if (argc > 1) {
        n = (size_t)(argc - 1);
        a = malloc(sizeof(unsigned long int) * n);
        if (NULL != a) {
            for (i = 1; i <= n; i++)
                a[i-1] = strtoul(argv[i], NULL, 10);
            printf("%lu\n", gcdn(a, n));
            free(a);
            return EXIT_SUCCESS;
        }
        return EXIT_FAILURE;
    }
    return EXIT_SUCCESS;
}