00001 /***************************************************************************** 00002 * Copyright (C) 2012 by Benjamin Hadorn (b_hadorn@bluewin.ch) 00003 ***************************************************************************** 00004 * Project : Zeus Base Library 00005 * Module : MathBigInteger 00006 * Package : Zeus.ZeusBase.Math 00007 * Author : Benjamin Hadorn 00008 * Date : 08.01.2012 00009 * System : Zeus-Framework 00010 ***************************************************************************** 00011 * Licence: * 00012 * This library is free software; you can redistribute it and/or modify * 00013 * it under the terms of the GNU Lesser General Public License as * 00014 * published by the Free Software Foundation; either version * 00015 * 2.1 of the License, or (at your option) any later version. * 00016 * * 00017 * This library is distributed in the hope that it will be useful, * 00018 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00019 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00020 * GNU Lesser General Public License for more details. * 00021 * * 00022 * You should have received a copy of the GNU Lesser General Public * 00023 * License along with this library; if not, write to the Free Software * 00024 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA * 00025 *****************************************************************************/ 00026 00027 /***************************************************************************** 00028 * Changes: 00029 *****************************************************************************/ 00030 00031 #ifndef MathBigIntegerHPP 00032 #define MathBigIntegerHPP 00033 00034 #include <zeusbase/System/BigInteger.hpp> 00035 00036 BEGIN_NAMESPACE_Zeus 00037 00038 /**************************************************************************/ 00041 /**************************************************************************/ 00042 template <int N> class TMathBigInteger 00043 { 00044 public: 00045 /**********************************************************************/ 00049 /**********************************************************************/ 00050 static TBigInteger<N> gcd(const TBigInteger<N>& rA, 00051 const TBigInteger<N>& rB, 00052 TBigInteger<N>& rX, 00053 TBigInteger<N>& rY) 00054 { 00055 rX = 1; 00056 rY = 0; 00057 TBigInteger<N> X1 = 0; 00058 TBigInteger<N> Y1 = 1; 00059 TBigInteger<N> A(rA); 00060 TBigInteger<N> B(rB); 00061 00062 while (!B.isZero()) 00063 { 00064 TBigInteger<N> Temp = B; 00065 00066 //Quotient = A / B; 00067 //B = A % B; 00068 TBigInteger<N> Quotient(A); 00069 Quotient.divideExt(Temp, B); //integer div 00070 00071 //B = A % B; 00072 //B = A.mod(B); 00073 A = Temp; 00074 00075 Temp = X1; 00076 //X1 = rX - Quotient * X1; 00077 X1 = Quotient; 00078 X1.multiply(Temp); 00079 X1.negate(); 00080 X1.add(rX); 00081 rX = Temp; 00082 00083 Temp = Y1; 00084 //Y1 = rY - Quotient * Y1; 00085 Y1 = Quotient; 00086 Y1.multiply(Temp); 00087 Y1.negate(); 00088 Y1.add(rY); 00089 rY = Temp; 00090 } 00091 return A; 00092 } 00093 }; 00094 00095 END_NAMESPACE_Zeus 00096 00097 #endif 00098