1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Modular Multiplicative Inverse</title> 5<link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css"> 6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 7<link rel="home" href="../index.html" title="Boost.Integer"> 8<link rel="up" href="../index.html" title="Boost.Integer"> 9<link rel="prev" href="extended_euclidean.html" title="Extended Euclidean Algorithm"> 10<link rel="next" href="mask.html" title="Integer Masks"> 11</head> 12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 13<table cellpadding="2" width="100%"><tr> 14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td> 15<td align="center"><a href="../../../../../index.html">Home</a></td> 16<td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td> 17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> 18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> 19<td align="center"><a href="../../../../../more/index.htm">More</a></td> 20</tr></table> 21<hr> 22<div class="spirit-nav"> 23<a accesskey="p" href="extended_euclidean.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="mask.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> 24</div> 25<div class="section"> 26<div class="titlepage"><div><div><h2 class="title" style="clear: both"> 27<a name="boost_integer.mod_inverse"></a><a class="link" href="mod_inverse.html" title="Modular Multiplicative Inverse">Modular Multiplicative Inverse</a> 28</h2></div></div></div> 29<div class="toc"><dl class="toc"> 30<dt><span class="section"><a href="mod_inverse.html#boost_integer.mod_inverse.introduction">Introduction</a></span></dt> 31<dt><span class="section"><a href="mod_inverse.html#boost_integer.mod_inverse.synopsis">Synopsis</a></span></dt> 32<dt><span class="section"><a href="mod_inverse.html#boost_integer.mod_inverse.usage">Usage</a></span></dt> 33<dt><span class="section"><a href="mod_inverse.html#boost_integer.mod_inverse.references">References</a></span></dt> 34</dl></div> 35<div class="section"> 36<div class="titlepage"><div><div><h3 class="title"> 37<a name="boost_integer.mod_inverse.introduction"></a><a class="link" href="mod_inverse.html#boost_integer.mod_inverse.introduction" title="Introduction">Introduction</a> 38</h3></div></div></div> 39<p> 40 The modular multiplicative inverse of a number <span class="emphasis"><em>a</em></span> is 41 that number <span class="emphasis"><em>x</em></span> which satisfies <span class="emphasis"><em>ax</em></span> 42 = 1 mod <span class="emphasis"><em>p</em></span>. A fast algorithm for computing modular multiplicative 43 inverses based on the extended Euclidean algorithm exists and is provided 44 by Boost. 45 </p> 46</div> 47<div class="section"> 48<div class="titlepage"><div><div><h3 class="title"> 49<a name="boost_integer.mod_inverse.synopsis"></a><a class="link" href="mod_inverse.html#boost_integer.mod_inverse.synopsis" title="Synopsis">Synopsis</a> 50</h3></div></div></div> 51<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">integer</span><span class="special">/</span><span class="identifier">mod_inverse</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 52 53<span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">integer</span> <span class="special">{</span> 54 55<span class="keyword">template</span><span class="special"><</span><span class="keyword">class</span> <span class="identifier">Z</span><span class="special">></span> 56<span class="identifier">Z</span> <span class="identifier">mod_inverse</span><span class="special">(</span><span class="identifier">Z</span> <span class="identifier">a</span><span class="special">,</span> <span class="identifier">Z</span> <span class="identifier">m</span><span class="special">);</span> 57 58<span class="special">}}</span> 59</pre> 60</div> 61<div class="section"> 62<div class="titlepage"><div><div><h3 class="title"> 63<a name="boost_integer.mod_inverse.usage"></a><a class="link" href="mod_inverse.html#boost_integer.mod_inverse.usage" title="Usage">Usage</a> 64</h3></div></div></div> 65<pre class="programlisting"><span class="keyword">int</span> <span class="identifier">x</span> <span class="special">=</span> <span class="identifier">mod_inverse</span><span class="special">(</span><span class="number">2</span><span class="special">,</span> <span class="number">5</span><span class="special">);</span> 66<span class="comment">// prints x = 3:</span> 67<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"x = "</span> <span class="special"><<</span> <span class="identifier">x</span> <span class="special"><<</span> <span class="string">"\n"</span><span class="special">;</span> 68 69<span class="keyword">int</span> <span class="identifier">y</span> <span class="special">=</span> <span class="identifier">mod_inverse</span><span class="special">(</span><span class="number">2</span><span class="special">,</span> <span class="number">4</span><span class="special">);</span> 70<span class="keyword">if</span> <span class="special">(</span><span class="identifier">y</span> <span class="special">==</span> <span class="number">0</span><span class="special">)</span> 71<span class="special">{</span> 72 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"There is no inverse of 2 mod 4\n"</span><span class="special">;</span> 73<span class="special">}</span> 74</pre> 75<p> 76 Multiplicative modular inverses exist if and only if <span class="emphasis"><em>a</em></span> 77 and <span class="emphasis"><em>m</em></span> are coprime. If <span class="emphasis"><em>a</em></span> and <span class="emphasis"><em>m</em></span> 78 share a common factor, then <code class="computeroutput"><span class="identifier">mod_inverse</span><span class="special">(</span><span class="identifier">a</span><span class="special">,</span> 79 <span class="identifier">m</span><span class="special">)</span></code> 80 returns zero. 81 </p> 82</div> 83<div class="section"> 84<div class="titlepage"><div><div><h3 class="title"> 85<a name="boost_integer.mod_inverse.references"></a><a class="link" href="mod_inverse.html#boost_integer.mod_inverse.references" title="References">References</a> 86</h3></div></div></div> 87<p> 88 Wagstaff, Samuel S., <span class="emphasis"><em>The Joy of Factoring</em></span>, Vol. 68. 89 American Mathematical Soc., 2013. 90 </p> 91</div> 92</div> 93<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 94<td align="left"></td> 95<td align="right"><div class="copyright-footer">Copyright © 2001-2009 Beman 96 Dawes, Daryle Walker, Gennaro Prota, John Maddock<p> 97 Distributed under the Boost Software License, Version 1.0. (See accompanying 98 file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) 99 </p> 100</div></td> 101</tr></table> 102<hr> 103<div class="spirit-nav"> 104<a accesskey="p" href="extended_euclidean.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="mask.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> 105</div> 106</body> 107</html> 108