Skip to content

Instantly share code, notes, and snippets.

@Prince781
Last active November 1, 2015 05:17
Show Gist options
  • Select an option

  • Save Prince781/3ccd1fcf7d379f9909f9 to your computer and use it in GitHub Desktop.

Select an option

Save Prince781/3ccd1fcf7d379f9909f9 to your computer and use it in GitHub Desktop.
gcd
/* gcd.c
* run `make gcd` */
#include <stdio.h>
#include <stdlib.h>
long gcd (long a, long b) {
ldiv_t qr;
while (b != 0) {
qr = ldiv(a, b);
a = b;
b = qr.rem;
}
return a;
}
int main(int argc, char *argv[]) {
if (argc != 3) {
printf ("usage: %s [a] [b]\n", argv[0]);
exit(1);
}
long a = atol(argv[1]);
long b = atol(argv[2]);
long val = gcd (a, b);
printf("gcd(%ld, %ld) = %ld\n", a, b, val);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment