00001 /************************************************************************** 00002 *** 00003 *** Copyright (c) 1995-2000 Regents of the University of California, 00004 *** Andrew E. Caldwell, Andrew B. Kahng and Igor L. Markov 00005 *** Copyright (c) 2000-2004 Regents of the University of Michigan, 00006 *** Saurabh N. Adya, Jarrod A. Roy and Igor L. Markov 00007 *** 00008 *** Contact author(s): abk@cs.ucsd.edu, imarkov@umich.edu 00009 *** Original Affiliation: UCLA, Computer Science Department, 00010 *** Los Angeles, CA 90095-1596 USA 00011 *** 00012 *** Permission is hereby granted, free of charge, to any person obtaining 00013 *** a copy of this software and associated documentation files (the 00014 *** "Software"), to deal in the Software without restriction, including 00015 *** without limitation 00016 *** the rights to use, copy, modify, merge, publish, distribute, sublicense, 00017 *** and/or sell copies of the Software, and to permit persons to whom the 00018 *** Software is furnished to do so, subject to the following conditions: 00019 *** 00020 *** The above copyright notice and this permission notice shall be included 00021 *** in all copies or substantial portions of the Software. 00022 *** 00023 *** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00024 *** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 00025 *** OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 00026 *** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 00027 *** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT 00028 *** OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR 00029 *** THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00030 *** 00031 *** 00032 ***************************************************************************/ 00033 00034 00035 00036 00037 00038 00039 00040 00041 // Created: Apr 26, 1998 by Dv 00042 // This file contains functions for finding gcd of 2, n unsigned numbers. 00043 00044 // CHANGES 00045 // 980512 ilm added abkFactorial() 00046 00047 #ifdef _MSC_VER 00048 #pragma warning(disable:4786) 00049 #endif 00050 00051 #include "abkcommon.h" 00052 00053 00054 unsigned abkGcd (unsigned x, unsigned y) 00055 { 00056 unsigned temp; 00057 00058 if (x < y) 00059 { 00060 std::swap(x, y); 00061 } 00062 00063 if (y == 0) return x; 00064 00065 while (y != 0) 00066 { 00067 temp = x % y; x = y; y = temp; 00068 } 00069 return x; 00070 } 00071 00072 unsigned abkGcd (const std::vector<unsigned>& numbers) 00073 { 00074 unsigned count = numbers.size(); 00075 abkfatal (count!=0, "Vector of size 0 passed to abkGcd"); 00076 00077 std::vector<unsigned> numsLocal(numbers); 00078 while (1) 00079 { 00080 if (count == 1) return numsLocal[0]; 00081 00082 unsigned i=0; 00083 for (; i < (count/2); i++) 00084 numsLocal[i] = abkGcd (numsLocal[2*i], numsLocal[2*i+1]); 00085 00086 if (2*i == count) count = i; 00087 else 00088 { 00089 numsLocal[i] = numsLocal[count-1]; 00090 count = i+1; 00091 } 00092 } 00093 } 00094 00095 unsigned abkFactorial(unsigned n) 00096 { 00097 unsigned fact=n--; 00098 for(;n!=1;n--) fact*=n; 00099 return fact; 00100 } 00101