Files
C-Plus-Plus/d8/d53/modular__inverse__fermat__little__theorem_8cpp.html
2024-10-21 13:23:10 +00:00

413 lines
27 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta name="generator" content="Doxygen 1.12.0"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Algorithms_in_C++: math/modular_inverse_fermat_little_theorem.cpp File Reference</title>
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<script type="text/javascript" src="../../clipboard.js"></script>
<link href="../../navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../navtreedata.js"></script>
<script type="text/javascript" src="../../navtree.js"></script>
<script type="text/javascript" src="../../resize.js"></script>
<script type="text/javascript" src="../../cookie.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
jax: ["input/TeX","output/HTML-CSS"],
});
</script>
<script type="text/javascript" async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML/MathJax.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr id="projectrow">
<td id="projectalign">
<div id="projectname">Algorithms_in_C++<span id="projectnumber">&#160;1.0.0</span>
</div>
<div id="projectbrief">Set of algorithms implemented in C++.</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.12.0 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "../../search/",'.html');
/* @license-end */
</script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function() { codefold.init(1); });
/* @license-end */
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function() {
initMenu('../../',true,false,'search.php','Search',true);
$(function() { init_search(); });
});
/* @license-end */
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
<div id="nav-tree">
<div id="nav-tree-contents">
<div id="nav-sync" class="sync"></div>
</div>
</div>
<div id="splitbar" style="-moz-user-select:none;"
class="ui-resizable-handle">
</div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function(){initNavTree('d8/d53/modular__inverse__fermat__little__theorem_8cpp.html','../../'); initResizable(true); });
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<div id="MSearchResults">
<div class="SRPage">
<div id="SRIndex">
<div id="SRResults"></div>
<div class="SRStatus" id="Loading">Loading...</div>
<div class="SRStatus" id="Searching">Searching...</div>
<div class="SRStatus" id="NoMatches">No Matches</div>
</div>
</div>
</div>
</div>
<div class="header">
<div class="summary">
<a href="#namespaces">Namespaces</a> &#124;
<a href="#func-members">Functions</a> </div>
<div class="headertitle"><div class="title">modular_inverse_fermat_little_theorem.cpp File Reference</div></div>
</div><!--header-->
<div class="contents">
<p>C++ Program to find the modular inverse using <a href="https://en.wikipedia.org/wiki/Fermat%27s_little_theorem" target="_blank">Fermat's Little Theorem</a>
<a href="#details">More...</a></p>
<div class="textblock"><code>#include &lt;cassert&gt;</code><br />
<code>#include &lt;cstdint&gt;</code><br />
<code>#include &lt;iostream&gt;</code><br />
</div><div class="textblock"><div class="dynheader">
Include dependency graph for modular_inverse_fermat_little_theorem.cpp:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d3/d1e/modular__inverse__fermat__little__theorem_8cpp__incl.svg" width="255" height="126"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
</div>
</div><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="namespaces" name="namespaces"></a>
Namespaces</h2></td></tr>
<tr class="memitem:"><td class="memItemLeft" align="right" valign="top">namespace &#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../dd/d47/namespacemath.html">math</a></td></tr>
<tr class="memdesc:dd/d47/namespacemath"><td class="mdescLeft">&#160;</td><td class="mdescRight">for IO operations <br /></td></tr>
<tr class="separator:"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:"><td class="memItemLeft" align="right" valign="top">namespace &#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d7/d50/namespacemodular__inverse__fermat.html">modular_inverse_fermat</a></td></tr>
<tr class="memdesc:d7/d50/namespacemodular__inverse__fermat"><td class="mdescLeft">&#160;</td><td class="mdescRight">Calculate modular inverse using Fermat's Little Theorem. <br /></td></tr>
<tr class="separator:"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="func-members" name="func-members"></a>
Functions</h2></td></tr>
<tr class="memitem:a1c62a87e79f29b98d8fe5b12eacdd928" id="r_a1c62a87e79f29b98d8fe5b12eacdd928"><td class="memItemLeft" align="right" valign="top"><a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/types/integer.html">std::int64_t</a>&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="#a1c62a87e79f29b98d8fe5b12eacdd928">math::modular_inverse_fermat::binExpo</a> (<a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/types/integer.html">std::int64_t</a> a, <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/types/integer.html">std::int64_t</a> b, <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/types/integer.html">std::int64_t</a> m)</td></tr>
<tr class="memdesc:a1c62a87e79f29b98d8fe5b12eacdd928"><td class="mdescLeft">&#160;</td><td class="mdescRight">Calculate exponent with modulo using binary exponentiation in \(O(\log b)\) time. <br /></td></tr>
<tr class="separator:a1c62a87e79f29b98d8fe5b12eacdd928"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:aba4929409fee35c3cb559e962a544b3e" id="r_aba4929409fee35c3cb559e962a544b3e"><td class="memItemLeft" align="right" valign="top">bool&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="#aba4929409fee35c3cb559e962a544b3e">math::modular_inverse_fermat::isPrime</a> (<a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/types/integer.html">std::int64_t</a> m)</td></tr>
<tr class="memdesc:aba4929409fee35c3cb559e962a544b3e"><td class="mdescLeft">&#160;</td><td class="mdescRight">Check if an integer is a prime number in \(O(\sqrt{m})\) time. <br /></td></tr>
<tr class="separator:aba4929409fee35c3cb559e962a544b3e"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ae7e807e02c65c6fffd6162b4c66290c2" id="r_ae7e807e02c65c6fffd6162b4c66290c2"><td class="memItemLeft" align="right" valign="top"><a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/types/integer.html">std::int64_t</a>&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="#ae7e807e02c65c6fffd6162b4c66290c2">math::modular_inverse_fermat::modular_inverse</a> (<a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/types/integer.html">std::int64_t</a> a, <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/types/integer.html">std::int64_t</a> m)</td></tr>
<tr class="memdesc:ae7e807e02c65c6fffd6162b4c66290c2"><td class="mdescLeft">&#160;</td><td class="mdescRight">calculates the modular inverse. <br /></td></tr>
<tr class="separator:ae7e807e02c65c6fffd6162b4c66290c2"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:aa8dca7b867074164d5f45b0f3851269d" id="r_aa8dca7b867074164d5f45b0f3851269d"><td class="memItemLeft" align="right" valign="top">static void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="#aa8dca7b867074164d5f45b0f3851269d">test</a> ()</td></tr>
<tr class="memdesc:aa8dca7b867074164d5f45b0f3851269d"><td class="mdescLeft">&#160;</td><td class="mdescRight">Self-test implementation. <br /></td></tr>
<tr class="separator:aa8dca7b867074164d5f45b0f3851269d"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ae66f6b31b5ad750f1fe042a706a4e3d4" id="r_ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="memItemLeft" align="right" valign="top">int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="#ae66f6b31b5ad750f1fe042a706a4e3d4">main</a> ()</td></tr>
<tr class="memdesc:ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="mdescLeft">&#160;</td><td class="mdescRight">Main function. <br /></td></tr>
<tr class="separator:ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table>
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
<div class="textblock"><p>C++ Program to find the modular inverse using <a href="https://en.wikipedia.org/wiki/Fermat%27s_little_theorem" target="_blank">Fermat's Little Theorem</a> </p>
<p>Fermat's Little Theorem state that </p><p class="formulaDsp">
\[ϕ(m) = m-1\]
</p>
<p> where \(m\) is a prime number. </p><p class="formulaDsp">
\begin{eqnarray*}
a \cdot x &amp;&amp; 1 \;\text{mod}\; m\\
x &amp;&amp; a^{-1} \;\text{mod}\; m
\end{eqnarray*}
</p>
<p> Using Euler's theorem we can modify the equation. </p><p class="formulaDsp">
\[
a^{ϕ(m)} ≡ 1 \;\text{mod}\; m
\]
</p>
<p> (Where '^' denotes the exponent operator)</p>
<p>Here 'ϕ' is Euler's Totient Function. For modular inverse existence 'a' and 'm' must be relatively primes numbers. To apply Fermat's Little Theorem is necessary that 'm' must be a prime number. Generally in many competitive programming competitions 'm' is either 1000000007 (1e9+7) or 998244353.</p>
<p>We considered m as large prime (1e9+7). \(a^{ϕ(m)} ≡ 1 \;\text{mod}\; m\) (Using Euler's Theorem) \(ϕ(m) = m-1\) using Fermat's Little Theorem. \(a^{m-1} ≡ 1 \;\text{mod}\; m\) Now multiplying both side by \(a^{-1}\). </p><p class="formulaDsp">
\begin{eqnarray*}
a^{m-1} \cdot a^{-1} &amp;&amp; a^{-1} \;\text{mod}\; m\\
a^{m-2} &amp;&amp; a^{-1} \;\text{mod}\; m
\end{eqnarray*}
</p>
<p>We will find the exponent using binary exponentiation such that the algorithm works in \(O(\log n)\) time.</p>
<p>Examples: -</p><ul>
<li>a = 3 and m = 7</li>
<li>\(a^{-1} \;\text{mod}\; m\) is equivalent to \(a^{m-2} \;\text{mod}\; m\)</li>
<li>\(3^5 \;\text{mod}\; 7 = 243 \;\text{mod}\; 7 = 5\) <br />
Hence, \(3^{-1} \;\text{mod}\; 7 = 5\) or \(3 \times 5 \;\text{mod}\; 7 = 1 \;\text{mod}\; 7\) (as \(a\times a^{-1} = 1\)) </li>
</ul>
</div><h2 class="groupheader">Function Documentation</h2>
<a id="a1c62a87e79f29b98d8fe5b12eacdd928" name="a1c62a87e79f29b98d8fe5b12eacdd928"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a1c62a87e79f29b98d8fe5b12eacdd928">&#9670;&#160;</a></span>binExpo()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname"><a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/types/integer.html">std::int64_t</a> math::modular_inverse_fermat::binExpo </td>
<td>(</td>
<td class="paramtype"><a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/types/integer.html">std::int64_t</a></td> <td class="paramname"><span class="paramname"><em>a</em></span>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype"><a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/types/integer.html">std::int64_t</a></td> <td class="paramname"><span class="paramname"><em>b</em></span>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype"><a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/types/integer.html">std::int64_t</a></td> <td class="paramname"><span class="paramname"><em>m</em></span>&#160;)</td>
</tr>
</table>
</div><div class="memdoc">
<p>Calculate exponent with modulo using binary exponentiation in \(O(\log b)\) time. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">a</td><td>The base </td></tr>
<tr><td class="paramname">b</td><td>The exponent </td></tr>
<tr><td class="paramname">m</td><td>The modulo </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>The result of \(a^{b} % m\) </dd></dl>
<div class="fragment"><div class="line"><span class="lineno"> 67</span> {</div>
<div class="line"><span class="lineno"> 68</span> a %= m;</div>
<div class="line"><span class="lineno"> 69</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/types/integer.html">std::int64_t</a> res = 1;</div>
<div class="line"><span class="lineno"> 70</span> <span class="keywordflow">while</span> (b &gt; 0) {</div>
<div class="line"><span class="lineno"> 71</span> <span class="keywordflow">if</span> (b % 2 != 0) {</div>
<div class="line"><span class="lineno"> 72</span> res = res * a % m;</div>
<div class="line"><span class="lineno"> 73</span> }</div>
<div class="line"><span class="lineno"> 74</span> a = a * a % m;</div>
<div class="line"><span class="lineno"> 75</span> <span class="comment">// Dividing b by 2 is similar to right shift by 1 bit</span></div>
<div class="line"><span class="lineno"> 76</span> b &gt;&gt;= 1;</div>
<div class="line"><span class="lineno"> 77</span> }</div>
<div class="line"><span class="lineno"> 78</span> <span class="keywordflow">return</span> res;</div>
<div class="line"><span class="lineno"> 79</span>}</div>
<div class="ttc" id="ainteger_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/types/integer.html">std::int64_t</a></div></div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d8/d53/modular__inverse__fermat__little__theorem_8cpp_a1c62a87e79f29b98d8fe5b12eacdd928_cgraph.svg" width="280" height="68"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
</div>
</div>
</div>
<a id="aba4929409fee35c3cb559e962a544b3e" name="aba4929409fee35c3cb559e962a544b3e"></a>
<h2 class="memtitle"><span class="permalink"><a href="#aba4929409fee35c3cb559e962a544b3e">&#9670;&#160;</a></span>isPrime()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">bool math::modular_inverse_fermat::isPrime </td>
<td>(</td>
<td class="paramtype"><a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/types/integer.html">std::int64_t</a></td> <td class="paramname"><span class="paramname"><em>m</em></span></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Check if an integer is a prime number in \(O(\sqrt{m})\) time. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">m</td><td>An intger to check for primality </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>true if the number is prime </dd>
<dd>
false if the number is not prime </dd></dl>
<div class="fragment"><div class="line"><span class="lineno"> 86</span> {</div>
<div class="line"><span class="lineno"> 87</span> <span class="keywordflow">if</span> (m &lt;= 1) {</div>
<div class="line"><span class="lineno"> 88</span> <span class="keywordflow">return</span> <span class="keyword">false</span>;</div>
<div class="line"><span class="lineno"> 89</span> } </div>
<div class="line"><span class="lineno"> 90</span> <span class="keywordflow">for</span> (<a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/types/integer.html">std::int64_t</a> i = 2; i * i &lt;= m; i++) {</div>
<div class="line"><span class="lineno"> 91</span> <span class="keywordflow">if</span> (m % i == 0) {</div>
<div class="line"><span class="lineno"> 92</span> <span class="keywordflow">return</span> <span class="keyword">false</span>;</div>
<div class="line"><span class="lineno"> 93</span> }</div>
<div class="line"><span class="lineno"> 94</span> }</div>
<div class="line"><span class="lineno"> 95</span> <span class="keywordflow">return</span> <span class="keyword">true</span>;</div>
<div class="line"><span class="lineno"> 96</span>}</div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d8/d53/modular__inverse__fermat__little__theorem_8cpp_aba4929409fee35c3cb559e962a544b3e_cgraph.svg" width="164" height="76"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
</div>
</div>
</div>
<a id="ae66f6b31b5ad750f1fe042a706a4e3d4" name="ae66f6b31b5ad750f1fe042a706a4e3d4"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ae66f6b31b5ad750f1fe042a706a4e3d4">&#9670;&#160;</a></span>main()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">int main </td>
<td>(</td>
<td class="paramtype">void</td> <td class="paramname"><span class="paramname"><em></em></span></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Main function. </p>
<dl class="section return"><dt>Returns</dt><dd>0 on exit </dd></dl>
<div class="fragment"><div class="line"><span class="lineno"> 137</span> {</div>
<div class="line"><span class="lineno"> 138</span> <a class="code hl_function" href="#aa8dca7b867074164d5f45b0f3851269d">test</a>(); <span class="comment">// run self-test implementation</span></div>
<div class="line"><span class="lineno"> 139</span> <span class="keywordflow">return</span> 0;</div>
<div class="line"><span class="lineno"> 140</span>}</div>
<div class="ttc" id="amodular__inverse__fermat__little__theorem_8cpp_html_aa8dca7b867074164d5f45b0f3851269d"><div class="ttname"><a href="#aa8dca7b867074164d5f45b0f3851269d">test</a></div><div class="ttdeci">static void test()</div><div class="ttdoc">Self-test implementation.</div><div class="ttdef"><b>Definition</b> modular_inverse_fermat_little_theorem.cpp:122</div></div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d8/d53/modular__inverse__fermat__little__theorem_8cpp_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.svg" width="152" height="36"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
</div>
</div>
</div>
<a id="ae7e807e02c65c6fffd6162b4c66290c2" name="ae7e807e02c65c6fffd6162b4c66290c2"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ae7e807e02c65c6fffd6162b4c66290c2">&#9670;&#160;</a></span>modular_inverse()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname"><a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/types/integer.html">std::int64_t</a> math::modular_inverse_fermat::modular_inverse </td>
<td>(</td>
<td class="paramtype"><a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/types/integer.html">std::int64_t</a></td> <td class="paramname"><span class="paramname"><em>a</em></span>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype"><a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/types/integer.html">std::int64_t</a></td> <td class="paramname"><span class="paramname"><em>m</em></span>&#160;)</td>
</tr>
</table>
</div><div class="memdoc">
<p>calculates the modular inverse. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">a</td><td>Integer value for the base </td></tr>
<tr><td class="paramname">m</td><td>Integer value for modulo </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>The result that is the modular inverse of a modulo m </dd></dl>
<div class="fragment"><div class="line"><span class="lineno"> 103</span> {</div>
<div class="line"><span class="lineno"> 104</span> <span class="keywordflow">while</span> (a &lt; 0) {</div>
<div class="line"><span class="lineno"> 105</span> a += m;</div>
<div class="line"><span class="lineno"> 106</span> }</div>
<div class="line"><span class="lineno"> 107</span> </div>
<div class="line"><span class="lineno"> 108</span> <span class="comment">// Check for invalid cases</span></div>
<div class="line"><span class="lineno"> 109</span> <span class="keywordflow">if</span> (!<a class="code hl_function" href="#aba4929409fee35c3cb559e962a544b3e">isPrime</a>(m) || a == 0) {</div>
<div class="line"><span class="lineno"> 110</span> <span class="keywordflow">return</span> -1; <span class="comment">// Invalid input</span></div>
<div class="line"><span class="lineno"> 111</span> }</div>
<div class="line"><span class="lineno"> 112</span> </div>
<div class="line"><span class="lineno"> 113</span> <span class="keywordflow">return</span> <a class="code hl_function" href="../../de/dcf/binary__exponent_8cpp.html#aeb48dce0725e63d19147944f41843c73">binExpo</a>(a, m - 2, m); <span class="comment">// Fermat&#39;s Little Theorem</span></div>
<div class="line"><span class="lineno"> 114</span>}</div>
<div class="ttc" id="abinary__exponent_8cpp_html_aeb48dce0725e63d19147944f41843c73"><div class="ttname"><a href="../../de/dcf/binary__exponent_8cpp.html#aeb48dce0725e63d19147944f41843c73">binExpo</a></div><div class="ttdeci">int binExpo(int a, int b)</div><div class="ttdef"><b>Definition</b> binary_exponent.cpp:28</div></div>
<div class="ttc" id="amodular__inverse__fermat__little__theorem_8cpp_html_aba4929409fee35c3cb559e962a544b3e"><div class="ttname"><a href="#aba4929409fee35c3cb559e962a544b3e">math::modular_inverse_fermat::isPrime</a></div><div class="ttdeci">bool isPrime(std::int64_t m)</div><div class="ttdoc">Check if an integer is a prime number in time.</div><div class="ttdef"><b>Definition</b> modular_inverse_fermat_little_theorem.cpp:86</div></div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d8/d53/modular__inverse__fermat__little__theorem_8cpp_ae7e807e02c65c6fffd6162b4c66290c2_cgraph.svg" width="380" height="150"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
</div>
</div>
</div>
<a id="aa8dca7b867074164d5f45b0f3851269d" name="aa8dca7b867074164d5f45b0f3851269d"></a>
<h2 class="memtitle"><span class="permalink"><a href="#aa8dca7b867074164d5f45b0f3851269d">&#9670;&#160;</a></span>test()</h2>
<div class="memitem">
<div class="memproto">
<table class="mlabels">
<tr>
<td class="mlabels-left">
<table class="memname">
<tr>
<td class="memname">static void test </td>
<td>(</td>
<td class="paramname"><span class="paramname"><em></em></span></td><td>)</td>
<td></td>
</tr>
</table>
</td>
<td class="mlabels-right">
<span class="mlabels"><span class="mlabel">static</span></span> </td>
</tr>
</table>
</div><div class="memdoc">
<p>Self-test implementation. </p>
<dl class="section return"><dt>Returns</dt><dd>void </dd></dl>
<div class="fragment"><div class="line"><span class="lineno"> 122</span> {</div>
<div class="line"><span class="lineno"> 123</span> assert(math::modular_inverse_fermat::modular_inverse(0, 97) == -1);</div>
<div class="line"><span class="lineno"> 124</span> assert(math::modular_inverse_fermat::modular_inverse(15, -2) == -1);</div>
<div class="line"><span class="lineno"> 125</span> assert(math::modular_inverse_fermat::modular_inverse(3, 10) == -1);</div>
<div class="line"><span class="lineno"> 126</span> assert(math::modular_inverse_fermat::modular_inverse(3, 7) == 5);</div>
<div class="line"><span class="lineno"> 127</span> assert(math::modular_inverse_fermat::modular_inverse(1, 101) == 1);</div>
<div class="line"><span class="lineno"> 128</span> assert(math::modular_inverse_fermat::modular_inverse(-1337, 285179) == 165519);</div>
<div class="line"><span class="lineno"> 129</span> assert(math::modular_inverse_fermat::modular_inverse(123456789, 998244353) == 25170271);</div>
<div class="line"><span class="lineno"> 130</span> assert(math::modular_inverse_fermat::modular_inverse(-9876543210, 1000000007) == 784794281);</div>
<div class="line"><span class="lineno"> 131</span>}</div>
</div><!-- fragment -->
</div>
</div>
</div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="navelem"><a class="el" href="../../dir_296d53ceaeaa7e099814a6def439fe8a.html">math</a></li><li class="navelem"><a class="el" href="../../d8/d53/modular__inverse__fermat__little__theorem_8cpp.html">modular_inverse_fermat_little_theorem.cpp</a></li>
<li class="footer">Generated by <a href="https://www.doxygen.org/index.html"><img class="footer" src="../../doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.12.0 </li>
</ul>
</div>
</body>
</html>