Computes the quotients and the convergents of a given fraction, which may be arbitrarily large.
- See also
- Instantiating fractions
# A huge fraction
$ ./examples/arithmetic/convergents-biginteger 243224233245235253407096734543059 4324213412343432913758138673203834
z = [0,17,1,3,1,1,12,1,2,33,2,1,1,1,1,49,1,1,1,1,17,34,1,1,304,1,2,1,1,1,2,1,48,1,20,2,3,5,1,1,16,9,1,1,5,1,2,2,7,4,3,1,7,1,1,17,1,1,29,1,12,2,5]
z_0 = 0 / 1
z_1 = 1 / 17
z_2 = 1 / 18
z_3 = 4 / 71
z_4 = 5 / 89
...
z_18 = 23610961 / 419772458
...
z_40 = 832739221613445323225 / 14805030169237188131024
...
z_62 = 243224233245235253407096734543059 / 4324213412343432913758138673203834
#include <iostream>
#include "DGtal/arithmetic/LighterSternBrocot.h"
void usage( int, char** argv )
{
std::cerr << "Usage: " << argv[ 0 ] << " <p> <q>" << std::endl;
std::cerr << "\t - computes the successive convergent of the fraction p / q." << std::endl;
}
int main(
int argc,
char** argv )
{
if ( argc < 3 )
{
usage( argc, argv );
return 1;
}
std::string inputP = argv[ 1 ];
std::string inputQ = argv[ 2 ];
typedef SB::Fraction Fraction;
typedef Fraction::Value
Value;
Fraction f( p, q );
std::cout << "z = ";
{
std::cout << ( ( it == itbegin ) ? "[" : "," )
<< u.first;
}
std::cout << "]" << std::endl;
Fraction g;
{
std::cout << "z_" << u.second << " = ";
g.push_back( u );
std::cout << g.p() << " / " << g.q() << std::endl;
}
return 0;
}
Aim: The Stern-Brocot tree is the tree of irreducible fractions. This class allows to construct it pr...
Point::Coordinate Integer
MyDigitalSurface::ConstIterator ConstIterator
DGtal is the top-level namespace which contains all DGtal functions and types.
boost::int64_t int64_t
signed 94-bit integer.
mpz_class BigInteger
Multi-precision integer with GMP implementation.