mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-04-01 01:32:32 +08:00
Documentation for 0931d530ae
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
<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.9.5"/>
|
||||
<meta name="generator" content="Doxygen 1.9.6"/>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1"/>
|
||||
<title>Algorithms_in_C++: math/modular_division.cpp File Reference</title>
|
||||
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
|
||||
@@ -41,7 +41,7 @@ MathJax.Hub.Config({
|
||||
</table>
|
||||
</div>
|
||||
<!-- end header part -->
|
||||
<!-- Generated by Doxygen 1.9.5 -->
|
||||
<!-- Generated by Doxygen 1.9.6 -->
|
||||
<script type="text/javascript">
|
||||
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
|
||||
var searchBox = new SearchBox("searchBox", "../../search/",'.html');
|
||||
@@ -127,20 +127,20 @@ Namespaces</h2></td></tr>
|
||||
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="func-members" name="func-members"></a>
|
||||
Functions</h2></td></tr>
|
||||
<tr class="memitem:a66cdf93153cbd1408bd74ac68961d179"><td class="memItemLeft" align="right" valign="top">uint64_t </td><td class="memItemRight" valign="bottom"><a class="el" href="../../df/d72/modular__division_8cpp.html#a66cdf93153cbd1408bd74ac68961d179">math::modular_division::power</a> (uint64_t a, uint64_t b, uint64_t c)</td></tr>
|
||||
<tr class="memdesc:a66cdf93153cbd1408bd74ac68961d179"><td class="mdescLeft"> </td><td class="mdescRight">This function calculates a raised to exponent b under modulo c using modular exponentiation. <a href="../../df/d72/modular__division_8cpp.html#a66cdf93153cbd1408bd74ac68961d179">More...</a><br /></td></tr>
|
||||
<tr class="memdesc:a66cdf93153cbd1408bd74ac68961d179"><td class="mdescLeft"> </td><td class="mdescRight">This function calculates a raised to exponent b under modulo c using modular exponentiation. <br /></td></tr>
|
||||
<tr class="separator:a66cdf93153cbd1408bd74ac68961d179"><td class="memSeparator" colspan="2"> </td></tr>
|
||||
<tr class="memitem:a905e368ae121beb7e7ea35349ddcdac7"><td class="memItemLeft" align="right" valign="top">uint64_t </td><td class="memItemRight" valign="bottom"><a class="el" href="../../df/d72/modular__division_8cpp.html#a905e368ae121beb7e7ea35349ddcdac7">math::modular_division::mod_division</a> (uint64_t a, uint64_t b, uint64_t p)</td></tr>
|
||||
<tr class="memdesc:a905e368ae121beb7e7ea35349ddcdac7"><td class="mdescLeft"> </td><td class="mdescRight">This function calculates modular division. <a href="../../df/d72/modular__division_8cpp.html#a905e368ae121beb7e7ea35349ddcdac7">More...</a><br /></td></tr>
|
||||
<tr class="memdesc:a905e368ae121beb7e7ea35349ddcdac7"><td class="mdescLeft"> </td><td class="mdescRight">This function calculates modular division. <br /></td></tr>
|
||||
<tr class="separator:a905e368ae121beb7e7ea35349ddcdac7"><td class="memSeparator" colspan="2"> </td></tr>
|
||||
<tr class="memitem:aa8dca7b867074164d5f45b0f3851269d"><td class="memItemLeft" align="right" valign="top">static void </td><td class="memItemRight" valign="bottom"><a class="el" href="../../df/d72/modular__division_8cpp.html#aa8dca7b867074164d5f45b0f3851269d">test</a> ()</td></tr>
|
||||
<tr class="separator:aa8dca7b867074164d5f45b0f3851269d"><td class="memSeparator" colspan="2"> </td></tr>
|
||||
<tr class="memitem:a0ddf1224851353fc92bfbff6f499fa97"><td class="memItemLeft" align="right" valign="top">int </td><td class="memItemRight" valign="bottom"><a class="el" href="../../df/d72/modular__division_8cpp.html#a0ddf1224851353fc92bfbff6f499fa97">main</a> (int argc, char *argv[])</td></tr>
|
||||
<tr class="memdesc:a0ddf1224851353fc92bfbff6f499fa97"><td class="mdescLeft"> </td><td class="mdescRight">Main function. <a href="../../df/d72/modular__division_8cpp.html#a0ddf1224851353fc92bfbff6f499fa97">More...</a><br /></td></tr>
|
||||
<tr class="memdesc:a0ddf1224851353fc92bfbff6f499fa97"><td class="mdescLeft"> </td><td class="mdescRight">Main function. <br /></td></tr>
|
||||
<tr class="separator:a0ddf1224851353fc92bfbff6f499fa97"><td class="memSeparator" colspan="2"> </td></tr>
|
||||
</table>
|
||||
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
|
||||
<div class="textblock"><p >An algorithm to divide two numbers under modulo p <a href="https://www.geeksforgeeks.org/modular-division" target="_blank">Modular Division</a> </p>
|
||||
<p >To calculate division of two numbers under modulo p Modulo operator is not distributive under division, therefore we first have to calculate the inverse of divisor using <a href="https://en.wikipedia.org/wiki/Fermat%27s_little_theorem" target="_blank">Fermat's little theorem</a> Now, we can multiply the dividend with the inverse of divisor and modulo is distributive over multiplication operation. Let, We have 3 numbers a, b, p To compute (a/b)p (a/b)p ≡ (a*(inverse(b)))p ≡ ((ap)*inverse(b)p)p NOTE: For the existence of inverse of 'b', 'b' and 'p' must be coprime For simplicity we take p as prime Time Complexity: O(log(b)) Example: ( 24 / 3 ) % 5 => 8 % 5 = 3 — (i) Now the inverse of 3 is 2 (24 * 2) % 5 = (24 % 5) * (2 % 5) = (4 * 2) % 5 = 3 — (ii) (i) and (ii) are equal hence the answer is correct. </p><dl class="section see"><dt>See also</dt><dd><a class="el" href="../../d8/d53/modular__inverse__fermat__little__theorem_8cpp.html" title="C++ Program to find the modular inverse using Fermat's Little Theorem">modular_inverse_fermat_little_theorem.cpp</a>, <a class="el" href="../../d0/d6d/modular__exponentiation_8cpp.html" title="C++ Program for Modular Exponentiation Iteratively.">modular_exponentiation.cpp</a> </dd></dl>
|
||||
<div class="textblock"><p>An algorithm to divide two numbers under modulo p <a href="https://www.geeksforgeeks.org/modular-division" target="_blank">Modular Division</a> </p>
|
||||
<p>To calculate division of two numbers under modulo p Modulo operator is not distributive under division, therefore we first have to calculate the inverse of divisor using <a href="https://en.wikipedia.org/wiki/Fermat%27s_little_theorem" target="_blank">Fermat's little theorem</a> Now, we can multiply the dividend with the inverse of divisor and modulo is distributive over multiplication operation. Let, We have 3 numbers a, b, p To compute (a/b)p (a/b)p ≡ (a*(inverse(b)))p ≡ ((ap)*inverse(b)p)p NOTE: For the existence of inverse of 'b', 'b' and 'p' must be coprime For simplicity we take p as prime Time Complexity: O(log(b)) Example: ( 24 / 3 ) % 5 => 8 % 5 = 3 — (i) Now the inverse of 3 is 2 (24 * 2) % 5 = (24 % 5) * (2 % 5) = (4 * 2) % 5 = 3 — (ii) (i) and (ii) are equal hence the answer is correct. </p><dl class="section see"><dt>See also</dt><dd><a class="el" href="../../d8/d53/modular__inverse__fermat__little__theorem_8cpp.html" title="C++ Program to find the modular inverse using Fermat's Little Theorem">modular_inverse_fermat_little_theorem.cpp</a>, <a class="el" href="../../d0/d6d/modular__exponentiation_8cpp.html" title="C++ Program for Modular Exponentiation Iteratively.">modular_exponentiation.cpp</a> </dd></dl>
|
||||
<dl class="section author"><dt>Author</dt><dd><a href="https://github.com/shubhamamsa" target="_blank">Shubham Yadav</a> </dd></dl>
|
||||
</div><h2 class="groupheader">Function Documentation</h2>
|
||||
<a id="a0ddf1224851353fc92bfbff6f499fa97" name="a0ddf1224851353fc92bfbff6f499fa97"></a>
|
||||
@@ -234,8 +234,8 @@ Here is the call graph for this function:</div>
|
||||
</dd>
|
||||
</dl>
|
||||
<dl class="section return"><dt>Returns</dt><dd>a/b modulo c </dd></dl>
|
||||
<p >Calculate the inverse of b</p>
|
||||
<p >Calculate the final result</p>
|
||||
<p>Calculate the inverse of b</p>
|
||||
<p>Calculate the final result</p>
|
||||
<div class="fragment"><div class="line"><span class="lineno"> 74</span> {</div>
|
||||
<div class="line"><span class="lineno"> 75</span> uint64_t inverse = <a class="code hl_function" href="../../df/def/power__for__huge__numbers_8cpp.html#ae249a2af508aa94266023ce8aa81426f">power</a>(b, p - 2, p) % p; <span class="comment">/// Calculate the inverse of b</span></div>
|
||||
<div class="line"><span class="lineno"> 76</span> uint64_t <a class="code hl_function" href="../../de/dc3/fibonacci__sum_8cpp.html#aadb40ac4c74a7efc0680b83eeee138aa">result</a> =</div>
|
||||
@@ -295,12 +295,12 @@ Here is the call graph for this function:</div>
|
||||
</dd>
|
||||
</dl>
|
||||
<dl class="section return"><dt>Returns</dt><dd>a raised to power b modulo c </dd></dl>
|
||||
<p >Initialize the answer to be returned</p>
|
||||
<p >Update a if it is more than or equal to c</p>
|
||||
<p >In case a is divisible by c;</p>
|
||||
<p >If b is odd, multiply a with answer</p>
|
||||
<p >b must be even now</p>
|
||||
<p >b = b/2</p>
|
||||
<p>Initialize the answer to be returned</p>
|
||||
<p>Update a if it is more than or equal to c</p>
|
||||
<p>In case a is divisible by c;</p>
|
||||
<p>If b is odd, multiply a with answer</p>
|
||||
<p>b must be even now</p>
|
||||
<p>b = b/2</p>
|
||||
<div class="fragment"><div class="line"><span class="lineno"> 49</span> {</div>
|
||||
<div class="line"><span class="lineno"> 50</span> uint64_t ans = 1; <span class="comment">/// Initialize the answer to be returned</span></div>
|
||||
<div class="line"><span class="lineno"> 51</span> a = a % c; <span class="comment">/// Update a if it is more than or equal to c</span></div>
|
||||
@@ -343,7 +343,7 @@ Here is the call graph for this function:</div>
|
||||
</tr>
|
||||
</table>
|
||||
</div><div class="memdoc">
|
||||
<p >Function for testing power function. test cases and assert statement. </p><dl class="section return"><dt>Returns</dt><dd><code>void</code> </dd></dl>
|
||||
<p>Function for testing power function. test cases and assert statement. </p><dl class="section return"><dt>Returns</dt><dd><code>void</code> </dd></dl>
|
||||
<div class="fragment"><div class="line"><span class="lineno"> 88</span> {</div>
|
||||
<div class="line"><span class="lineno"> 89</span> uint64_t test_case_1 = <a class="code hl_function" href="../../df/d72/modular__division_8cpp.html#a905e368ae121beb7e7ea35349ddcdac7">math::modular_division::mod_division</a>(8, 2, 2);</div>
|
||||
<div class="line"><span class="lineno"> 90</span> assert(test_case_1 == 0);</div>
|
||||
@@ -379,7 +379,7 @@ Here is the call graph for this function:</div>
|
||||
<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="../../df/d72/modular__division_8cpp.html">modular_division.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.9.5 </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.9.6 </li>
|
||||
</ul>
|
||||
</div>
|
||||
</body>
|
||||
|
||||
Reference in New Issue
Block a user