DGtal 1.3.0
Loading...
Searching...
No Matches
Functions
extended-euclid.cpp File Reference
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <iomanip>
#include <string>
#include "DGtal/arithmetic/IntegerComputer.h"

Go to the source code of this file.

Functions

void usage (int, char **argv)
 
int main (int argc, char **argv)
 

Detailed Description

Author
Jacques-Olivier Lachaud (jacqu.nosp@m.es-o.nosp@m.livie.nosp@m.r.la.nosp@m.chaud.nosp@m.@uni.nosp@m.v-sav.nosp@m.oie..nosp@m.fr ) Laboratory of Mathematics (CNRS, UMR 5127), University of Savoie, France
Date
2012/02/06

An example file named extended-euclid.

This file is part of the DGtal library.

Definition in file extended-euclid.cpp.

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

Main.

[extended-euclid-types]

[extended-euclid-types]

[extended-euclid-instantiation]

[extended-euclid-instantiation]

[extended-euclid-process]

[extended-euclid-process]

Definition at line 57 of file extended-euclid.cpp.

58{
59 if ( argc < 4 )
60 {
61 usage( argc, argv );
62 return 0;
63 }
64
66 typedef BigInteger Integer;
67 typedef IntegerComputer<Integer> IC;
69
71 Integer a( argv[ 1 ] );
72 Integer b( argv[ 2 ] );
73 Integer c( argv[ 3 ] );
74 IC ic;
75 Integer g = ic.gcd( a, b );
76 if ( ic.isZero( a ) || ic.isZero( b ) )
77 {
78 std::cerr << "[Error] parameters a and b should be non-null." << std::endl;
79 return 1;
80 }
81 if ( ic.isNotZero( c % g ) )
82 {
83 std::cerr << "[Error] parameter c should be a multiple of gcd(a,b)." << std::endl;
84 return 2;
85 }
87
89 IC::Vector2I X = ic.extendedEuclid( a, b, c );
90 std::cout << "x=" << X[ 0 ] << " y=" << X[ 1 ] << std::endl;
91 Integer d = a*X[ 0 ] + b*X[ 1 ];
92 if ( c != d )
93 {
94 std::cerr << "[Internal Error] Output of extended Euclid algorithm is incorrect: " << d << " != " << c
95 << ". This should not happen." << std::endl;
96 return 3;
97 }
99 return 0;
100}
Aim: This class gathers several types and methods to make computation with integers.
mpz_class BigInteger
Multi-precision integer with GMP implementation.
Definition: BasicTypes.h:79

◆ usage()

void usage ( int  ,
char **  argv 
)

Definition at line 47 of file extended-euclid.cpp.

48{
49 std::cerr << "Usage: " << argv[ 0 ] << " <a> <b> <c>" << std::endl;
50 std::cerr << "\t - solves the diophantine equation ax+by=c by the extended Euclid algorithm." << std::endl;
51 std::cerr << "\t - note: c should be a multiple of gcd(a,b)." << std::endl;
52}