I’d replied earlier, twice, from my android phone, but the messages appear to
have gone astray.
If a rational is a repeating decimal, with period n+1, p< q, then, for
example,
p/q = 0.000...a0a1...an a0a1...an...
= 10^k. sum( ai. (sum {j=0, infinity} 1 / 10^( n’i))), n’ = n + 1,
and the infinite sum of 1 / 10^n’i is 1000.../999... with n’ 0s and 9s.
So 999... * p / q = 10^k. sum( ai )
So finding the size of the first 999... which is divisible by q gives the
period.
This seems to do the trick, with some precautions for non-repeating decimals,
such a as 1/2, for which _1 is returned:
RMAX =: 10000 NB. arbitrary maximum to avoid endless loop
NB. predict period of repeating decimal representation of a rational number
rep =: 3 : 0
if. 128 = 3!:0 ] y do. NB. if input is a j rational
rep/ 2 x: y
else.
rep/y
end.
:
'p q' =. (%+./) x,y NB. reduce x%y to lowest terms
q =. q <.@% _ q: inv 1 0 1 * 3 q: q NB. remove powers of 2 & 5 from q
num =. (9&>.&.(10&#.^:_1)) q NB. lowest 10 ^ n - 1 > q
if. q | num + 1 do. NB. if a repeating decimal ....
r =. >:<. 10 ^. num NB. length of seed numerator, 999.... = 10^n
- 1
NB. find the first 9999... divisible by q
while. (r <: RMAX) * diff =. q | num do.
num =. 9 + 10 * diff
r =. >: r
end.
else.
_1
end.
)
A few examples:
rep every 1000r7;(1 7);3 777777; 3r777777;300000 777; 1 5
6 6 42 42 6 _1
Mike
Nb. I’ve clipped the rather long history, with apologies to Roger and Skip.
Sent from my iPad
> On 12 Jan 2020, at 05:58, Skip Cave <[email protected]> wrote:
>
> Found what i needed to discover the repetition index of rational decimal
> expansions.:
>
>
> *]n=. x:%p:3+i.20*
>
> *1r7 1r11 1r13 1r17 1r19 1r23 1r29 1r31 1r37 1r41 1r43 1r47 1r53 1r59 1r61
> 1r67 1r71 1r73 1r79 1r83*
>
> NB. Verb for finding repetition index:
>
>
> * rpt =.3 :'~.|-/"1]2,\I.>+./ea*./"1 ea=ea({10,.i.500)$ea{3}.0j1000":y'*
>
> * rpt ea n*
>
> *┌─┬─┬─┬──┬──┬──┬──┬──┬─┬─┬──┬──┬──┬──┬──┬──┬──┬─┬──┬──┐*
>
> *│6│2│6│16│18│22│28│15│3│5│21│46│13│58│60│33│35│8│13│41│*
>
> *└─┴─┴─┴──┴──┴──┴──┴──┴─┴─┴──┴──┴──┴──┴──┴──┴──┴─┴──┴──┘*
>
> Skip
>
> Skip Cave
> Cave Consulting LLC
>
>
>> On Sat, Jan 11, 2020 at 10:00 PM Skip Cave <[email protected]> wrote:
>>
>> I'll need something like this:
>>
>> n=.'abcdefgabcdefgabcdefgabcdefgabcdefgabcdefg'
>>
>> nc=.n="1
>> |:n,.(1|.n),.(2|.n),.(3|.n),.(4|.n),.(5|.n),.(6|.n),.(7|.n),.(8|.n),.(9|.n),.(10|.n)
>>
>> +/"1 nc
>>
>> 42 0 0 0 0 0 0 42 0 0 0
>>
>> So it is clear, n repeats every 8 digits.
>>
>>
>> There's got to be a non-looping way to define nc more concisely.
>>
>>
>> Skip
>>
>> Skip Cave
>> Cave Consulting LLC
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm