summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--gcd-gmp.c81
-rw-r--r--gcd.c64
2 files changed, 78 insertions, 67 deletions
diff --git a/gcd-gmp.c b/gcd-gmp.c
index 355c0cc..41213cc 100644
--- a/gcd-gmp.c
+++ b/gcd-gmp.c
@@ -9,56 +9,61 @@
#include <stdio.h>
#include <gmp.h>
-void gcd2(mpz_t r, mpz_t a1, mpz_t b1)
+void
+gcd2 (mpz_t r, mpz_t a1, mpz_t b1)
{
- mpz_t a, b;
- mpz_init_set(a, a1);
- mpz_init_set(b, b1);
- while (mpz_sgn(b) != 0) {
- mpz_set(r, b); /* r = b; */
- mpz_mod(b, a, b); /* b = a % b; */
- mpz_set(a, r); /* a = r; */
+ mpz_t a, b;
+ mpz_init_set (a, a1);
+ mpz_init_set (b, b1);
+ while (mpz_sgn (b) != 0)
+ {
+ mpz_set (r, b); /* r = b; */
+ mpz_mod (b, a, b); /* b = a % b; */
+ mpz_set (a, r); /* a = r; */
}
- mpz_set(r, a);
- mpz_clear(a);
- mpz_clear(b);
+ mpz_set (r, a);
+ mpz_clear (a);
+ mpz_clear (b);
}
-void gcdn(mpz_t r, mpz_t a[], size_t n)
+void
+gcdn (mpz_t r, mpz_t a[], size_t n)
{
- size_t i;
- mpz_set(r, a[0]);
- for (i = 1; i < n; i++)
- gcd2(r, r, a[i]); /* mpz_gcd ;-) */
+ size_t i;
+ mpz_set (r, a[0]);
+ for (i = 1; i < n; i++)
+ gcd2 (r, r, a[i]); /* mpz_gcd ;-) */
}
-int main (int argc, char *argv[])
+int
+main (int argc, char *argv[])
{
- mpz_t *a, g;
- int i, n;
+ mpz_t *a, g;
+ int i, n;
- if (argc > 1) {
- n = argc - 1;
- a = malloc(sizeof(mpz_t) * n);
- if (NULL != a) {
- for (i = 1; i <= n; i++)
- mpz_init_set_str(a[i-1], argv[i], 10);
+ if (argc > 1)
+ {
+ n = argc - 1;
+ a = malloc (sizeof (mpz_t) * n);
+ if (NULL != a)
+ {
+ for (i = 1; i <= n; i++)
+ mpz_init_set_str (a[i - 1], argv[i], 10);
- mpz_init(g);
- gcdn(g, a, n);
- mpz_out_str(NULL, 10, g);
- printf("\n");
+ mpz_init (g);
+ gcdn (g, a, n);
+ mpz_out_str (NULL, 10, g);
+ printf ("\n");
- /* No need actually before exit */
- mpz_clear(g);
- for (i = 1; i <= n; i++)
- mpz_clear(a[i-1]);
- free(a);
- return EXIT_SUCCESS;
+ /* No need actually before exit */
+ mpz_clear (g);
+ for (i = 1; i <= n; i++)
+ mpz_clear (a[i - 1]);
+ free (a);
+ return EXIT_SUCCESS;
}
- return EXIT_FAILURE;
+ return EXIT_FAILURE;
}
- return EXIT_SUCCESS;
+ return EXIT_SUCCESS;
}
-
diff --git a/gcd.c b/gcd.c
index ea99592..227be89 100644
--- a/gcd.c
+++ b/gcd.c
@@ -1,46 +1,52 @@
#include <stdlib.h>
#include <stdio.h>
-static unsigned long int gcd2(unsigned long int a, unsigned long int b)
+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;
+ unsigned long int c;
+ while (b != 0)
+ {
+ c = b;
+ b = a % b;
+ a = c;
}
- return a;
+ return a;
}
-static unsigned long int gcdn(unsigned long int *a, size_t n)
+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]);
+ unsigned long int r;
+ size_t i;
+ r = a[0];
+ for (i = 1; i < n; i++)
+ {
+ r = gcd2 (r, a[i]);
}
- return r;
+ return r;
}
-int main (int argc, char *argv[])
+int
+main (int argc, char *argv[])
{
- unsigned long int *a;
- size_t i, n;
+ unsigned long int *a;
+ size_t i, n;
- if (argc > 1) {
- n = (size_t)(argc - 1);
- a = (unsigned long int*) 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;
+ if (argc > 1)
+ {
+ n = (size_t) (argc - 1);
+ a = (unsigned long int *) 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_FAILURE;
}
- return EXIT_SUCCESS;
+ return EXIT_SUCCESS;
}
-