//----------------------------------------------------------------------------- // Copyright © 2003 - Philip Howard - All rights reserved // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License // as published by the Free Software Foundation; either version 2 // of the License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. //----------------------------------------------------------------------------- // package libh/arith // homepage http://libh.slashusr.org/ //----------------------------------------------------------------------------- // author Philip Howard // email libh at ipal dot org // homepage http://phil.ipal.org/ //----------------------------------------------------------------------------- // This file is best viewed using a fixed spaced font such as Courier // and in a display at least 120 columns wide. //----------------------------------------------------------------------------- #include #include #include //----------------------------------------------------------------------------- // proram testgcd // // purpose Determine the greatest common denominator by Euclid's algorithm // for type unsigned long long and display the steps. // // syntax testgcd number1 number2 //----------------------------------------------------------------------------- int main ( int argc , char * * argv , char * * envp ) { #if HAVE_C99 unsigned long long a; unsigned long long b; #else unsigned long a; unsigned long b; #endif //-- Make sure we have arguments. if ( argc <= 2 ) { fprintf( stderr, "syntax: testgcd number1 number2\n" ); return 1; } //-- Convert arguments. #if HAVE_C99 a = strtoull( argv[1], NULL, 0 ); b = strtoull( argv[2], NULL, 0 ); #else a = strtoul( argv[1], NULL, 0 ); b = strtoul( argv[2], NULL, 0 ); #endif //-- Make sure a has the larger value. if ( a < b ) { unsigned long long c; c = a; a = b; b = c; } //-- Do non-recursive version of Euclid's algorithm. while ( b ) { #if HAVE_C99 unsigned long long c; printf( "%20llu %20llu\n", a, b ); #else unsigned long c; printf( "%10lu %10lu\n", a, b ); #endif c = a % b; a = b; b = c; } #if HAVE_C99 printf( "%20llu\n", a ); #else printf( "%10lu\n", a ); #endif return 0; }