Simple Application Framework  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Saf/Math/Algebra/Number.h
Go to the documentation of this file.
00001 /*
00002     This file is part of Simple Application Framework (Saf) library.
00003     Copyright (C) 2010 - 2012 Ondrej Danek <ondrej.danek@gmail.com>
00004 
00005     This library is free software: you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published 
00007     by the Free Software Foundation, either version 3 of the License, or
00008     (at your option) any later version.
00009 
00010     Saf is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with Simple Application Framework library. If not, 
00017     see <http://www.gnu.org/licenses/>.
00018 */
00019 
00028 #ifndef SAF_MATH_ALGEBRA_NUMBER_H
00029 #define SAF_MATH_ALGEBRA_NUMBER_H
00030 
00031 #include "../../Type.h"
00032 #include "../Basic.h"
00033 #include "../../Type/IteratorTraits.h"
00034 
00035 namespace Saf
00036 {
00037     namespace Math
00038     {
00039         namespace Algebra
00040         {
00042             template <class T>
00043             T Gcd(T a, T b)
00044             {
00045                 a = Abs(a);
00046                 b = Abs(b);
00047 
00048                 while (b != 0)
00049                 {
00050                     T t = b;
00051                     b = a % b;
00052                     a = t;
00053                 }
00054 
00055                 return a;
00056             }
00057 
00059             template <class T>
00060             T Gcd(T a, T b, T c)
00061             {
00062                 return Gcd(a, Gcd(b,c));
00063             }
00064 
00066             template <class FwdIter>
00067             typename Type::IteratorTraits<FwdIter>::ValType Gcd(FwdIter begin, FwdIter end)
00068             {
00069                 typename Type::IteratorTraits<FwdIter>::ValType t = 0;
00070 
00071                 while (begin != end)
00072                 {
00073                     t = Gcd(t, *begin);
00074                     ++begin;
00075                 }
00076 
00077                 return t;
00078             }
00079 
00081             template <class T>
00082             T Lcm(T a, T b)
00083             {
00084                 return (a / Gcd(a,b)) * b;
00085             }
00086 
00088             template <class T>
00089             T Lcm(T a, T b, T c)
00090             {     
00091                 return Lcm(a, Lcm(b,c));
00092             }
00093 
00095             template <class FwdIter>
00096             typename Type::IteratorTraits<FwdIter>::ValType Lcm(FwdIter begin, FwdIter end)
00097             {
00098                 typename Type::IteratorTraits<FwdIter>::ValType t = 1;
00099 
00100                 while (begin != end)
00101                 {
00102                     t = Lcm(t, *begin);
00103                     ++begin;
00104                 }
00105 
00106                 return t;
00107             }
00108         }
00109     }
00110 }
00111 
00112 #endif