<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: Modular Arithmetic</title>
	<atom:link href="http://programmingpraxis.com/2009/07/07/modular-arithmetic/feed/" rel="self" type="application/rss+xml" />
	<link>http://programmingpraxis.com/2009/07/07/modular-arithmetic/</link>
	<description>A collection of etudes, updated weekly, for the education and enjoyment of the savvy programmer</description>
	<lastBuildDate>Mon, 28 May 2012 03:30:45 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: Lii</title>
		<link>http://programmingpraxis.com/2009/07/07/modular-arithmetic/#comment-3947</link>
		<dc:creator><![CDATA[Lii]]></dc:creator>
		<pubDate>Sat, 03 Dec 2011 17:25:59 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=907#comment-3947</guid>
		<description><![CDATA[Here is a variant of the above code where the operation take and return functions. The intention is to get rid of the ugliness of having to pass the modulo value in every function invocation.

The disadvantage is that you cant use ints directly in the expressions, you have to wrap them in the ugly i functions. Is there a solution to this?

&lt;code&gt;
test_func = ((i 5) *% (i 6) +% (i 7)) 13

(=%) :: TModNum -&gt; TModNum -&gt; (Integer -&gt; Bool)
a =% b = \m -&gt; (a m) == (b m)

type TModNum = (Integer -&gt; Integer)

(+%), (-%), (*%), (/%) :: TModNum -&gt; TModNum -&gt; TModNum

a +% b = \m -&gt; mod ((a m) + (b m)) m
a -% b = \m -&gt; mod ((a m) - (b m)) m
a *% b = \m -&gt; mod ((a m) * (b m)) m
a /% b = \m -&gt; mod ((a m) * (inv (b m) m)) m

i :: Integer -&gt; (Integer -&gt; Integer)
i n = \m -&gt; mod n m
&lt;/code&gt;]]></description>
		<content:encoded><![CDATA[<p>Here is a variant of the above code where the operation take and return functions. The intention is to get rid of the ugliness of having to pass the modulo value in every function invocation.</p>
<p>The disadvantage is that you cant use ints directly in the expressions, you have to wrap them in the ugly i functions. Is there a solution to this?</p>
<p><code><br />
test_func = ((i 5) *% (i 6) +% (i 7)) 13</p>
<p>(=%) :: TModNum -&gt; TModNum -&gt; (Integer -&gt; Bool)<br />
a =% b = \m -&gt; (a m) == (b m)</p>
<p>type TModNum = (Integer -&gt; Integer)</p>
<p>(+%), (-%), (*%), (/%) :: TModNum -&gt; TModNum -&gt; TModNum</p>
<p>a +% b = \m -&gt; mod ((a m) + (b m)) m<br />
a -% b = \m -&gt; mod ((a m) - (b m)) m<br />
a *% b = \m -&gt; mod ((a m) * (b m)) m<br />
a /% b = \m -&gt; mod ((a m) * (inv (b m) m)) m</p>
<p>i :: Integer -&gt; (Integer -&gt; Integer)<br />
i n = \m -&gt; mod n m<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: programmingpraxis</title>
		<link>http://programmingpraxis.com/2009/07/07/modular-arithmetic/#comment-668</link>
		<dc:creator><![CDATA[programmingpraxis]]></dc:creator>
		<pubDate>Mon, 12 Oct 2009 19:08:32 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=907#comment-668</guid>
		<description><![CDATA[Here is an improved version of modular inverse, based on Algorithm 9.4.4 from the book &lt;em&gt;Prime Numbers: A Computational Perspective&lt;/em&gt; by Richard Crandall and Carl Pomerance:

&lt;code&gt;(define (inverse x m)
&#160;&#160;(if (not (= (gcd x m) 1))
&#160;&#160;&#160;&#160;&#160;&#160;(error &#039;inverse &quot;divisor must be coprime to modulus&quot;)
&#160;&#160;&#160;&#160;&#160;&#160;(let loop ((z (modulo x m)) (a 1))
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;(if (= z 1) a
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;(let ((q (- (quotient m z))))
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;(loop (+ m (* q z)) (modulo (* q a) m)))))))&lt;/code&gt;]]></description>
		<content:encoded><![CDATA[<p>Here is an improved version of modular inverse, based on Algorithm 9.4.4 from the book <em>Prime Numbers: A Computational Perspective</em> by Richard Crandall and Carl Pomerance:</p>
<p><code>(define (inverse x m)<br />
&nbsp;&nbsp;(if (not (= (gcd x m) 1))<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(error &#39;inverse &quot;divisor must be coprime to modulus&quot;)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(let loop ((z (modulo x m)) (a 1))<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(if (= z 1) a<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(let ((q (- (quotient m z))))<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(loop (+ m (* q z)) (modulo (* q a) m)))))))</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Posts about Programming from google blogs as of July 8, 2009 « tryfly.com</title>
		<link>http://programmingpraxis.com/2009/07/07/modular-arithmetic/#comment-301</link>
		<dc:creator><![CDATA[Posts about Programming from google blogs as of July 8, 2009 « tryfly.com]]></dc:creator>
		<pubDate>Wed, 08 Jul 2009 23:46:48 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=907#comment-301</guid>
		<description><![CDATA[[...] OLE 2 or ActiveX, we are referring to a very specific type of object, sometimes called a &#8230;   Modular Arithmetic « Programming Praxis - programmingpraxis.com 12/31/1969 Programming Praxis. A collection of etudes, updated weekly, for [...]]]></description>
		<content:encoded><![CDATA[<p>[...] OLE 2 or ActiveX, we are referring to a very specific type of object, sometimes called a &#8230;   Modular Arithmetic « Programming Praxis &#8211; programmingpraxis.com 12/31/1969 Programming Praxis. A collection of etudes, updated weekly, for [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Remco Niemeijer</title>
		<link>http://programmingpraxis.com/2009/07/07/modular-arithmetic/#comment-298</link>
		<dc:creator><![CDATA[Remco Niemeijer]]></dc:creator>
		<pubDate>Wed, 08 Jul 2009 09:27:57 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=907#comment-298</guid>
		<description><![CDATA[My Haskell solution (see http://bonsaicode.wordpress.com/2009/07/08/programming-praxis-modular-arithmetic/ for a version with comments):

[sourcecode lang=&#039;css&#039;]
import Data.Bits
import Data.List

euclid :: Integral a =&gt; a -&gt; a -&gt; a
euclid x y = f 1 0 x 0 1 y where
    f a b g u v w &#124; w == 0    = mod a y
                  &#124; otherwise = f u v w (a-q*u) (b-q*v) (g-q*w)
                                where q = div g w

inv :: Integral a =&gt; a -&gt; a -&gt; a
inv x m &#124; gcd x m == 1 = euclid x m
        &#124; otherwise    = error &quot;divisor must be coprime to modulus&quot;

(&lt;=&gt;) :: Integral a =&gt; a -&gt; a -&gt; a -&gt; Bool
a &lt;=&gt; b = \m -&gt; mod a m == mod b m

(&lt;+&gt;), (&lt;-&gt;), (&lt;*&gt;), (&lt;/&gt;) :: Integral a =&gt; a -&gt; a -&gt; a -&gt; a
a &lt;+&gt; b = \m -&gt; mod (a + mod b m) m
a &lt;-&gt; b = a &lt;+&gt; (-b)
a &lt;*&gt; b = \m -&gt; mod (a * mod b m) m
a &lt;/&gt; b = \m -&gt; (a &lt;*&gt; inv b m) m

expm :: Integer -&gt; Integer -&gt; Integer -&gt; Integer  
expm b e m = foldl&#039; (\r (b&#039;, _) -&gt; mod (r * b&#039;) m) 1 .  
             filter (flip testBit 0 . snd) .  
             zip (iterate (flip mod m . (^ 2)) b) $
             takeWhile (&gt; 0) $ iterate (`shiftR` 1) e

(&lt;^&gt;) :: Integer -&gt; Integer -&gt; Integer -&gt; Integer
(&lt;^&gt;) = expm

sqrtm :: Integral a =&gt; a -&gt; a -&gt; [a]
sqrtm x m = case [n &#124; n &lt;- [1..m], (n &lt;*&gt; n) m == mod x m] of
                (_:_:_:_) -&gt; []
                s         -&gt; s

modulo :: a -&gt; (a -&gt; b) -&gt; b
modulo = flip ($)

main :: IO ()
main = do mapM_ (modulo 12) [
              print . (17 &lt;=&gt; 5),
              print . (8 &lt;+&gt; 9),
              print . (4 &lt;-&gt; 9),
              print . (3 &lt;*&gt; 7),
              print . (9 &lt;/&gt; 7)]
          mapM_ (modulo 13) [ 
              print . (6 &lt;^&gt; 2),
              print . (7 &lt;^&gt; 2),
              print . sqrtm 10]
[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>My Haskell solution (see <a href="http://bonsaicode.wordpress.com/2009/07/08/programming-praxis-modular-arithmetic/" rel="nofollow">http://bonsaicode.wordpress.com/2009/07/08/programming-praxis-modular-arithmetic/</a> for a version with comments):</p>
<pre class="brush: css;">
import Data.Bits
import Data.List

euclid :: Integral a =&gt; a -&gt; a -&gt; a
euclid x y = f 1 0 x 0 1 y where
    f a b g u v w | w == 0    = mod a y
                  | otherwise = f u v w (a-q*u) (b-q*v) (g-q*w)
                                where q = div g w

inv :: Integral a =&gt; a -&gt; a -&gt; a
inv x m | gcd x m == 1 = euclid x m
        | otherwise    = error &quot;divisor must be coprime to modulus&quot;

(&lt;=&gt;) :: Integral a =&gt; a -&gt; a -&gt; a -&gt; Bool
a &lt;=&gt; b = \m -&gt; mod a m == mod b m

(&lt;+&gt;), (&lt;-&gt;), (&lt;*&gt;), (&lt;/&gt;) :: Integral a =&gt; a -&gt; a -&gt; a -&gt; a
a &lt;+&gt; b = \m -&gt; mod (a + mod b m) m
a &lt;-&gt; b = a &lt;+&gt; (-b)
a &lt;*&gt; b = \m -&gt; mod (a * mod b m) m
a &lt;/&gt; b = \m -&gt; (a &lt;*&gt; inv b m) m

expm :: Integer -&gt; Integer -&gt; Integer -&gt; Integer  
expm b e m = foldl' (\r (b', _) -&gt; mod (r * b') m) 1 .  
             filter (flip testBit 0 . snd) .  
             zip (iterate (flip mod m . (^ 2)) b) $
             takeWhile (&gt; 0) $ iterate (`shiftR` 1) e

(&lt;^&gt;) :: Integer -&gt; Integer -&gt; Integer -&gt; Integer
(&lt;^&gt;) = expm

sqrtm :: Integral a =&gt; a -&gt; a -&gt; [a]
sqrtm x m = case [n | n &lt;- [1..m], (n &lt;*&gt; n) m == mod x m] of
                (_:_:_:_) -&gt; []
                s         -&gt; s

modulo :: a -&gt; (a -&gt; b) -&gt; b
modulo = flip ($)

main :: IO ()
main = do mapM_ (modulo 12) [
              print . (17 &lt;=&gt; 5),
              print . (8 &lt;+&gt; 9),
              print . (4 &lt;-&gt; 9),
              print . (3 &lt;*&gt; 7),
              print . (9 &lt;/&gt; 7)]
          mapM_ (modulo 13) [ 
              print . (6 &lt;^&gt; 2),
              print . (7 &lt;^&gt; 2),
              print . sqrtm 10]
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Programming Praxis &#8211; Modular Arithmetic &#171; Bonsai Code</title>
		<link>http://programmingpraxis.com/2009/07/07/modular-arithmetic/#comment-297</link>
		<dc:creator><![CDATA[Programming Praxis &#8211; Modular Arithmetic &#171; Bonsai Code]]></dc:creator>
		<pubDate>Wed, 08 Jul 2009 09:27:19 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=907#comment-297</guid>
		<description><![CDATA[[...] Praxis &#8211; Modular&#160;Arithmetic By Remco Niemeijer  Yesterday’s Programming Praxis problem is about making a convenient way to do modular arithmetic. While the [...]]]></description>
		<content:encoded><![CDATA[<p>[...] Praxis &#8211; Modular&nbsp;Arithmetic By Remco Niemeijer  Yesterday’s Programming Praxis problem is about making a convenient way to do modular arithmetic. While the [...]</p>
]]></content:encoded>
	</item>
</channel>
</rss>

