Back to index

lightning-sunbird  0.9+nobinonly
gcd.c
Go to the documentation of this file.
00001 /*
00002  *  gcd.c
00003  *
00004  *  Greatest common divisor
00005  *
00006  * ***** BEGIN LICENSE BLOCK *****
00007  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00008  *
00009  * The contents of this file are subject to the Mozilla Public License Version
00010  * 1.1 (the "License"); you may not use this file except in compliance with
00011  * the License. You may obtain a copy of the License at
00012  * http://www.mozilla.org/MPL/
00013  *
00014  * Software distributed under the License is distributed on an "AS IS" basis,
00015  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00016  * for the specific language governing rights and limitations under the
00017  * License.
00018  *
00019  * The Original Code is the MPI Arbitrary Precision Integer Arithmetic library.
00020  *
00021  * The Initial Developer of the Original Code is
00022  * Michael J. Fromberger.
00023  * Portions created by the Initial Developer are Copyright (C) 1998
00024  * the Initial Developer. All Rights Reserved.
00025  *
00026  * Contributor(s):
00027  *
00028  * Alternatively, the contents of this file may be used under the terms of
00029  * either the GNU General Public License Version 2 or later (the "GPL"), or
00030  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00031  * in which case the provisions of the GPL or the LGPL are applicable instead
00032  * of those above. If you wish to allow use of your version of this file only
00033  * under the terms of either the GPL or the LGPL, and not to allow others to
00034  * use your version of this file under the terms of the MPL, indicate your
00035  * decision by deleting the provisions above and replace them with the notice
00036  * and other provisions required by the GPL or the LGPL. If you do not delete
00037  * the provisions above, a recipient may use your version of this file under
00038  * the terms of any one of the MPL, the GPL or the LGPL.
00039  *
00040  * ***** END LICENSE BLOCK ***** */
00041 /* $Id: gcd.c,v 1.3 2004/04/27 23:04:37 gerv%gerv.net Exp $ */
00042 
00043 #include <stdio.h>
00044 #include <stdlib.h>
00045 #include <string.h>
00046 
00047 #include "mpi.h"
00048 
00049 char     *g_prog = NULL;
00050 
00051 void print_mp_int(mp_int *mp, FILE *ofp);
00052 
00053 int main(int argc, char *argv[]) 
00054 {
00055   mp_int   a, b, x, y;
00056   mp_err   res;
00057   int      ext = 0;
00058 
00059   g_prog = argv[0];
00060 
00061   if(argc < 3) {
00062     fprintf(stderr, "Usage: %s <a> <b>\n", g_prog);
00063     return 1;
00064   }
00065 
00066   mp_init(&a); mp_read_radix(&a, argv[1], 10);
00067   mp_init(&b); mp_read_radix(&b, argv[2], 10);
00068 
00069   /* If we were called 'xgcd', compute x, y so that g = ax + by */
00070   if(strcmp(g_prog, "xgcd") == 0) {
00071     ext = 1;
00072     mp_init(&x); mp_init(&y);
00073   }
00074 
00075   if(ext) {
00076     if((res = mp_xgcd(&a, &b, &a, &x, &y)) != MP_OKAY) {
00077       fprintf(stderr, "%s: error: %s\n", g_prog, mp_strerror(res));
00078       mp_clear(&a); mp_clear(&b);
00079       mp_clear(&x); mp_clear(&y);
00080       return 1;
00081     }
00082   } else {
00083     if((res = mp_gcd(&a, &b, &a)) != MP_OKAY) {
00084       fprintf(stderr, "%s: error: %s\n", g_prog,
00085              mp_strerror(res));
00086       mp_clear(&a); mp_clear(&b);
00087       return 1;
00088     }
00089   }
00090 
00091   print_mp_int(&a, stdout); 
00092   if(ext) {
00093     fputs("x = ", stdout); print_mp_int(&x, stdout); 
00094     fputs("y = ", stdout); print_mp_int(&y, stdout); 
00095   }
00096 
00097   mp_clear(&a); mp_clear(&b);
00098 
00099   if(ext) {
00100     mp_clear(&x);
00101     mp_clear(&y);
00102   }
00103 
00104   return 0;
00105 
00106 }
00107 
00108 void print_mp_int(mp_int *mp, FILE *ofp)
00109 {
00110   char  *buf;
00111   int    len;
00112 
00113   len = mp_radix_size(mp, 10);
00114   buf = calloc(len, sizeof(char));
00115   mp_todecimal(mp, buf);
00116   fprintf(ofp, "%s\n", buf);
00117   free(buf);
00118 
00119 }