Just for fun: seen on the seqfan forum.
The sowing technique performed below could recall the Oware game.
Let's start with the integer 541, for example - which will be seen as three bowls
containing respectively 5, 4 and 1 seed (there are as many bowls as figures in the
original integer). At the start of a "game" one takes all the seeds of the
leftmost bowl, which are then sowed one by one to the right (the last bowl on the right
is followed by the first bowl on the left). The bowl where the sowing ends is also the
one by which the next sowing begins. And so on. Here is how the 541 game starts (and
ends); the yellow color marks the starting bowl and the one where the sowing ends:
5 4 1 0
1 6 3 2
2 7 1 2
3 7 0 0
The column at the right gives the starting index(instead of the yellow
color marks in the text above).
A straightforward method to generate the loop is as follows:
owareX=: 3 :0
r=. # s=. y
z=.i.0 0
j=.0
whilst. (0~:j) +.-. y -: s do.
n=.j{s
s=.0 j } s
for. i.n do.
j=.r|j+1
s=. j (>:@{)`[`]} s
end.
z=.z,s
end.
)
owareX 5 4 1
1 6 3
2 7 1
3 7 0
1 8 1
0 9 1
...
1 9 0
0 10 0
3 3 4
5 4 1
The above example has a loop length of 51.
Of course I'm not very satisfied with the inner (explicit) loop and the
speed.
To give you some idea of speed: https://oeis.org/A216476 has a PARI
program. Calling PARI/GP from J (using 'shell') with the following
expression:
(mind line wrapping)
arg=.'n=799989;{my(o=n=Vecsmall(Str(n)), c, p=Mod(0, #n)); until(!p &
o==n, c++; for(i=1,n[lift(p)+1]-n[lift(p)+1]=48, n[lift(p++)+1]++)); c}'
ts'it=.GP <arg' [1]
12.9263 14400
it
1534716
The idea is to match or even beat that timing with J. Here's my best
attempt so far (only counts loop length):
oware3C=: 3 :0
db=. 9,~,/(<9) 1&+ >/~i.r=. # s=. y
c=.j=.0
whilst. (0~:j) +.-. y -: s do.
s=. (0 j } s)+(-j+1)|.db{~d=.j{s
j=.r|j+d
c=.1+c
end.
)
ts 'oware3C 7 9 9 9 8 9'
30.9805 13440
less than 2.5x slower.
Anyway, something to chew on for those interested.
[1] GP is a verb of mine that streamlines calling PARI/GP with 'shell'.
It also does limited converting of GP output.
--
Met vriendelijke groet,
@@i = Arie Groeneveld
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm