mirror of
				https://github.com/boostorg/integer.git
				synced 2025-11-04 02:01:38 +01:00 
			
		
		
		
	
		
			
	
	
		
			48 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
	
	
		
		
			
		
	
	
			48 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
	
	
| 
								 | 
							
								[section:extended_euclidean Extended Euclidean Algorithm]
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								[section Introduction]
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								The extended Euclidean algorithm solves the integer relation /mx + ny/ = gcd(/m/, /n/) for /x/ and /y/.
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								[endsect]
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								[section Synopsis]
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    #include <boost/integer/extended_euclidean.hpp>
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    namespace boost { namespace integer {
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        template<class Z>
							 | 
						||
| 
								 | 
							
								        std::tuple<Z, Z, Z> extended_euclidean(Z m, Z n);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    }}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								[endsect]
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								[section Usage]
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								The tuple returned by the extended Euclidean algorithm contains, the greatest common divisor, /x/, and /y/, in that order:
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    int m = 12;
							 | 
						||
| 
								 | 
							
								    int n = 15;
							 | 
						||
| 
								 | 
							
								    auto tup = extended_euclidean(m, n);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    int gcd = std::get<0>(tup);
							 | 
						||
| 
								 | 
							
								    int x = std::get<1>(tup);
							 | 
						||
| 
								 | 
							
								    int y = std::get<2>(tup);
							 | 
						||
| 
								 | 
							
								    // mx + ny = gcd(m,n)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								[endsect]
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								[section References]
							 | 
						||
| 
								 | 
							
								Wagstaff, Samuel S., ['The Joy of Factoring], Vol. 68. American Mathematical Soc., 2013.
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								[endsect]
							 | 
						||
| 
								 | 
							
								[endsect]
							 | 
						||
| 
								 | 
							
								[/
							 | 
						||
| 
								 | 
							
								Copyright 2018 Nick Thompson.
							 | 
						||
| 
								 | 
							
								Distributed under the Boost Software License, Version 1.0.
							 | 
						||
| 
								 | 
							
								(See accompanying file LICENSE_1_0.txt or copy at
							 | 
						||
| 
								 | 
							
								http://www.boost.org/LICENSE_1_0.txt).
							 | 
						||
| 
								 | 
							
								]
							 |