<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: modulus code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 12 Oct 2008 21:01:45 GMT</pubDate>
    <description>DZone Snippets: modulus code</description>
    <item>
      <title>Simple Efficient Modpow</title>
      <link>http://snippets.dzone.com/posts/show/6073</link>
      <description>Raise large numbers to large exponents in modulus space.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def modpow(base, power, modulus)&lt;br /&gt;  m=base&lt;br /&gt;  pow = power.to_s(2)&lt;br /&gt;  1.upto(pow.length - 1) do |i|&lt;br /&gt;    m *= m&lt;br /&gt;    m %= modulus&lt;br /&gt;    if pow[i] == "1"[0]&lt;br /&gt;      m *= base&lt;br /&gt;      m %= modulus&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;  m&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 14 Sep 2008 02:11:29 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/6073</guid>
      <author>chrislee35 (Chris Lee)</author>
    </item>
    <item>
      <title>Modular inverse function</title>
      <link>http://snippets.dzone.com/posts/show/4256</link>
      <description>This function calculates &amp; returns the inverse of &lt;I&gt;a modulo n&lt;/I&gt;, both of which should be positive.  If the inverse does not exist, 0 is returned.  If you don't know what this means, don't bother.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;int modInverse(int a, int n) {&lt;br /&gt; int i = n, v = 0, d = 1;&lt;br /&gt; while (a&gt;0) {&lt;br /&gt;  int t = i/a, x = a;&lt;br /&gt;  a = i % x;&lt;br /&gt;  i = x;&lt;br /&gt;  x = d;&lt;br /&gt;  d = v - t*x;&lt;br /&gt;  v = x;&lt;br /&gt; }&lt;br /&gt; v %= n;&lt;br /&gt; if (v&lt;0) v = (v+n)%n;&lt;br /&gt; return v;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 05 Jul 2007 03:42:06 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4256</guid>
      <author>Minimiscience (Guildorn Tanaleth)</author>
    </item>
  </channel>
</rss>
