Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>OK, 3rd try's a charm :)</p> <p>I can't believe I forgot about modular exponentiation.</p> <p>So to steal/summarize from my 2nd answer, the nth digit of x/y is the 1st digit of (10<sup>n-1</sup>x mod y)/y = floor(10 * (10<sup>n-1</sup>x mod y) / y) mod 10.</p> <p>The part that takes all the time is the 10<sup>n-1</sup> mod y, but we can do that with fast (O(log n)) modular exponentiation. With this in place, it's not worth trying to do the cycle-finding algorithm. </p> <p>However, you do need the ability to do (a * b mod y) where a and b are numbers that may be as large as y. (if y requires 32 bits, then you need to do 32x32 multiply and then 64-bit % 32-bit modulus, or you need an algorithm that circumvents this limitation. See my listing that follows, since I ran into this limitation with Javascript.)</p> <p>So here's a new version.</p> <pre><code>function abmody(a,b,y) { var x = 0; // binary fun here while (a &gt; 0) { if (a &amp; 1) x = (x + b) % y; b = (2 * b) % y; a &gt;&gt;&gt;= 1; } return x; } function digits2(x,y,n1,n2) { // the nth digit of x/y = floor(10 * (10^(n-1)*x mod y) / y) mod 10. var m = n1-1; var A = 1, B = 10; while (m &gt; 0) { // loop invariant: 10^(n1-1) = A*(B^m) mod y if (m &amp; 1) { // A = (A * B) % y but javascript doesn't have enough sig. digits A = abmody(A,B,y); } // B = (B * B) % y but javascript doesn't have enough sig. digits B = abmody(B,B,y); m &gt;&gt;&gt;= 1; } x = x % y; // A = (A * x) % y; A = abmody(A,x,y); var answer = ""; for (var i = n1; i &lt;= n2; ++i) { var digit = Math.floor(10*A/y)%10; answer += digit; A = (A * 10) % y; } return answer; } </code></pre> <p>(You'll note that the structures of <code>abmody()</code> and the modular exponentiation are the same; both are based on <a href="http://en.wikipedia.org/wiki/Ancient_Egyptian_multiplication#Peasant_multiplication" rel="noreferrer">Russian peasant multiplication</a>.) And results:</p> <pre><code>js&gt;digits2(2124679,214748367,214748300,214748400) 20513882650385881630475914166090026658968726872786883636698387559799232373208220950057329190307649696 js&gt;digits2(122222,990000,100,110) 65656565656 js&gt;digits2(1,7,1,7) 1428571 js&gt;digits2(1,7,601,607) 1428571 js&gt;digits2(2124679,2147483647,2147484600,2147484700) 04837181235122113132440537741612893408915444001981729642479554583541841517920532039329657349423345806 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload