Added functions to compute the greatest common denominator: gcd, lgcd

This commit is contained in:
Andreas Huggel 2004-05-08 07:01:48 +00:00
parent b614abf06f
commit 89aea87b01
2 changed files with 45 additions and 3 deletions

View File

@ -20,14 +20,14 @@
*/
/*
File: types.cpp
Version: $Name: $ $Revision: 1.5 $
Version: $Name: $ $Revision: 1.6 $
Author(s): Andreas Huggel (ahu) <ahuggel@gmx.net>
History: 26-Jan-04, ahu: created
11-Feb-04, ahu: isolated as a component
*/
// *****************************************************************************
#include "rcsid.hpp"
EXIV2_RCSID("@(#) $Name: $ $Revision: 1.5 $ $RCSfile: types.cpp,v $")
EXIV2_RCSID("@(#) $Name: $ $Revision: 1.6 $ $RCSfile: types.cpp,v $")
// *****************************************************************************
// included header files
@ -232,4 +232,34 @@ namespace Exif {
os << std::dec << std::setfill(' ');
} // hexdump
int gcd(int a, int b)
{
int temp;
if (a < b) {
temp = a;
a = b;
b = temp;
}
while ((temp = a % b) != 0) {
a = b;
b = temp;
}
return b;
} // gcd
long lgcd(long a, long b)
{
long temp;
if (a < b) {
temp = a;
a = b;
b = temp;
}
while ((temp = a % b) != 0) {
a = b;
b = temp;
}
return b;
} // lgcd
} // namespace Exif

View File

@ -21,7 +21,7 @@
/*!
@file types.hpp
@brief Type definitions for Exiv2 and related functionality
@version $Name: $ $Revision: 1.8 $
@version $Name: $ $Revision: 1.9 $
@author Andreas Huggel (ahu)
<a href="mailto:ahuggel@gmx.net">ahuggel@gmx.net</a>
@date 09-Jan-04, ahu: created
@ -195,6 +195,18 @@ namespace Exif {
offset.
*/
void hexdump(std::ostream& os, const char* buf, long len, long offset =0);
/*!
@brief Return the greatest common denominator of integers a and b.
Both parameters must be greater than 0.
*/
int gcd(int a, int b);
/*!
@brief Return the greatest common denominator of long values a and b.
Both parameters must be greater than 0.
*/
long lgcd(long a, long b);
// *****************************************************************************
// template and inline definitions