<table cellspacing="0" cellpadding="0" border="0" ><tr><td valign="top" style="font: inherit;">Thanks, all.<div><br></div><div>I have an evaluation copy of Mathematica and have been looking for problems to feed it.</div><div><br></div><div>Michael</div><div><br>--- On <b>Mon, 6/27/11, Brent Yorgey <i>&lt;byorgey@seas.upenn.edu&gt;</i></b> wrote:<br><blockquote style="border-left: 2px solid rgb(16, 16, 255); margin-left: 5px; padding-left: 5px;"><br>From: Brent Yorgey &lt;byorgey@seas.upenn.edu&gt;<br>Subject: Re: [Haskell-cafe] Fwd: Re:  Period of a sequence<br>To: haskell-cafe@haskell.org<br>Date: Monday, June 27, 2011, 9:56 AM<br><br><div class="plainMail">I've attached some code I wrote a while ago for playing with repeating<br>decimal expansions, perhaps you'll find some of it useful.<br><br>-Brent<br><br>On Mon, Jun 27, 2011 at 02:21:55PM +0200, Steffen Schuldenzucker wrote:<br>&gt; <br>&gt; Michael,<br>&gt; <br>&gt; On 06/27/2011 01:51 PM, Steffen
 Schuldenzucker wrote:<br>&gt; &gt;<br>&gt; &gt; Forwarding to -cafe<br>&gt; &gt;<br>&gt; &gt; -------- Original Message --------<br>&gt; &gt; Subject: Re: [Haskell-cafe] Period of a sequence<br>&gt; &gt; Date: Mon, 27 Jun 2011 04:46:10 -0700 (PDT)<br>&gt; &gt; From: michael rice &lt;<a ymailto="mailto:nowgate@yahoo.com" href="/mc/compose?to=nowgate@yahoo.com">nowgate@yahoo.com</a>&gt;<br>&gt; &gt; To: Steffen Schuldenzucker &lt;<a ymailto="mailto:sschuldenzucker@uni-bonn.de" href="/mc/compose?to=sschuldenzucker@uni-bonn.de">sschuldenzucker@uni-bonn.de</a>&gt;<br>&gt; &gt;<br>&gt; &gt; Hi Steffen,<br>&gt; &gt;<br>&gt; &gt; Repeating decimals.<br>&gt; &gt;<br>&gt; &gt; 5/7 == 0.714285 714285 7142857 ... Period = 6<br>&gt; &gt;<br>&gt; &gt; It does seem like a difficult problem.<br>&gt; &gt;<br>&gt; &gt; This one is eventually repeating, with Period = 3<br>&gt; &gt;<br>&gt; &gt; 3227/555 = 5.8144 144 144…<br>&gt; <br>&gt; why not use the well-known
 division algorithm: (I hope this is readable)<br>&gt; <br>&gt; 3227 / 555<br>&gt; = 3227 `div` 555 + 3227 `mod` 555 / 555<br>&gt; = 5 + 452 / 555<br>&gt; = 5 + 0.1 * 4520 / 555<br>&gt; = 5 + 0.1 * (4520 `div` 555 + (4520 `mod` 555) / 555)<br>&gt; = 5 + 0.1 * (8 + 80 / 555)<br>&gt; = 5 + 0.1 * (8 + 0.1 * (800 / 555))<br>&gt; = 5 + 0.1 * (8 + 0.1 * (800 `div` 555 + (800 `mod` 555) / 555))<br>&gt; = 5 + 0.1 * (8 + 0.1 * (1 + 245 / 555))<br>&gt; = 5 + 0.1 * (8 + 0.1 * (1 + 0.1 * 2450 / 555))<br>&gt; = 5 + 0.1 * (8 + 0.1 * (1 + 0.1 * (4 + 230 / 555)))<br>&gt; = 5 + 0.1 * (8 + 0.1 * (1 + 0.1 * (4 + 0.1 * 2300 / 555)))<br>&gt; = 5 + 0.1 * (8 + 0.1 * (1 + 0.1 * (4 + 0.1 * (4 + 80 / 555))))<br>&gt; *whoops*, saw 80 already, namely in line 6. Would go on like that<br>&gt; forever if I continued like this, so the final result has to be:<br>&gt; <br>&gt; vvv Part before the place where I saw the '80' first<br>&gt; 5.8 144 144 144 ...<br>&gt;&nbsp;
 &nbsp;&nbsp;&nbsp;^^^ Part after I saw the '80'<br>&gt; <br>&gt; So you could write a recursive function that takes as an accumulating<br>&gt; parameter containing the list of numbers already seen:<br>&gt; <br>&gt; -- periodOf n m gives the periodic part of n/m as a decimal fraction.<br>&gt; -- (or an empty list if that number has finitely many decimal places)<br>&gt; &gt; periodOf :: (Integral a) =&gt; a -&gt; a -&gt; [a]<br>&gt; &gt; periodOf = periodOfWorker []<br>&gt; &gt;&nbsp;&nbsp;&nbsp;where<br>&gt; &gt;&nbsp; &nbsp;&nbsp;&nbsp;periodOfWorker seen n m<br>&gt; &gt;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;| n `mod` m == 0 = ...<br>&gt; &gt;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;| (n `mod` m) `elem` seen = ...<br>&gt; &gt;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;| otherwise = ...<br>&gt; <br>&gt; &gt;--- On *Mon, 6/27/11, Steffen Schuldenzucker<br>&gt; &gt;/&lt;<a ymailto="mailto:sschuldenzucker@uni-bonn.de"
 href="/mc/compose?to=sschuldenzucker@uni-bonn.de">sschuldenzucker@uni-bonn.de</a>&gt;/*wrote:<br>&gt; &gt;<br>&gt; &gt;<br>&gt; &gt;From: Steffen Schuldenzucker &lt;<a ymailto="mailto:sschuldenzucker@uni-bonn.de" href="/mc/compose?to=sschuldenzucker@uni-bonn.de">sschuldenzucker@uni-bonn.de</a>&gt;<br>&gt; &gt;Subject: Re: [Haskell-cafe] Period of a sequence<br>&gt; &gt;To: "michael rice" &lt;<a ymailto="mailto:nowgate@yahoo.com" href="/mc/compose?to=nowgate@yahoo.com">nowgate@yahoo.com</a>&gt;<br>&gt; &gt;Cc: <a ymailto="mailto:haskell-cafe@haskell.org" href="/mc/compose?to=haskell-cafe@haskell.org">haskell-cafe@haskell.org</a><br>&gt; &gt;Date: Monday, June 27, 2011, 4:32 AM<br>&gt; &gt;<br>&gt; &gt;<br>&gt; &gt;<br>&gt; &gt;On 06/26/2011 04:16 PM, michael rice wrote:<br>&gt; &gt; &gt; MathWorks has the function seqperiod(x) to return the period of<br>&gt; &gt;sequence<br>&gt; &gt; &gt; x. Is there an equivalent function in Haskell?<br>&gt;
 &gt;<br>&gt; &gt;Could you specify what exactly the function is supposed to do? I am<br>&gt; &gt;pretty sure that a function like<br>&gt; &gt;<br>&gt; &gt;seqPeriod :: (Eq a) =&gt; [a] -&gt; Maybe Integer -- Nothing iff non-periodic<br>&gt; &gt;<br>&gt; &gt;cannot be written. If "sequences" are represented by the terms that<br>&gt; &gt;define them (or this information is at least accessible), chances<br>&gt; &gt;might be better, but I would still be interested how such a function<br>&gt; &gt;works. The problem seems undecidable to me in general.<br>&gt; &gt;<br>&gt; &gt;On finite lists (which may be produced from infinite ones via<br>&gt; &gt;'take'), a naive implementation could be this:<br>&gt; &gt;<br>&gt; &gt; &gt;<br>&gt; &gt; &gt; import Data.List (inits, cycle, isPrefixOf)<br>&gt; &gt; &gt; import Debug.Trace<br>&gt; &gt; &gt;<br>&gt; &gt; &gt; -- Given a finite list, calculate its period.<br>&gt; &gt; &gt; -- The first parameter controls what is
 accepted as a generator.<br>&gt; &gt;See below.<br>&gt; &gt; &gt; -- Set it to False when looking at chunks from an infinite sequence.<br>&gt; &gt; &gt; listPeriod :: (Eq a) =&gt; Bool -&gt; [a] -&gt; Int<br>&gt; &gt; &gt; listPeriod precisely xs = case filter (generates precisely xs)<br>&gt; &gt;(inits xs) of<br>&gt; &gt; &gt; -- as (last $ init xs) == xs, this will always suffice.<br>&gt; &gt; &gt; (g:_) -&gt; length g -- length of the *shortest* generator<br>&gt; &gt; &gt;<br>&gt; &gt; &gt; -- @generates prec xs g@ iff @g@ generates @xs@ by repitition. If<br>&gt; &gt;@prec@, the<br>&gt; &gt; &gt; -- lengths have to match, too. Consider<br>&gt; &gt; &gt; --<br>&gt; &gt; &gt; -- &gt;&gt;&gt; generates True [1,2,3,1,2,1,2] [1,2,3,1,2]<br>&gt; &gt; &gt; -- False<br>&gt; &gt; &gt; --<br>&gt; &gt; &gt; -- &gt;&gt;&gt; generates False [1,2,3,1,2,1,2] [1,2,3,1,2]<br>&gt; &gt; &gt; -- True<br>&gt; &gt; &gt; generates :: (Eq a) =&gt; Bool -&gt; [a] -&gt; [a]
 -&gt; Bool<br>&gt; &gt; &gt; generates precisely xs g = if null g<br>&gt; &gt; &gt; then null xs<br>&gt; &gt; &gt; else (not precisely || length xs `mod` length g == 0)<br>&gt; &gt; &gt; &amp;&amp; xs `isPrefixOf` cycle g<br>&gt; &gt; &gt;<br>&gt; &gt;<br>&gt; &gt;-- Steffen<br>&gt; &gt;<br>&gt; &gt;<br>&gt; &gt;_______________________________________________<br>&gt; &gt;Haskell-Cafe mailing list<br>&gt; &gt;<a ymailto="mailto:Haskell-Cafe@haskell.org" href="/mc/compose?to=Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>&gt; &gt;<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>&gt; <br>&gt; _______________________________________________<br>&gt; Haskell-Cafe mailing list<br>&gt; <a ymailto="mailto:Haskell-Cafe@haskell.org" href="/mc/compose?to=Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>&gt; <a
 href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br></div><br>-----Inline Attachment Follows-----<br><br><div class="plainMail">_______________________________________________<br>Haskell-Cafe mailing list<br><a ymailto="mailto:Haskell-Cafe@haskell.org" href="/mc/compose?to=Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br><a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br></div></blockquote></div></td></tr></table>