ok. this is an iterative version faster and does not overflow.
jose2 =. ((>:@{:@:] | {.@[ + }:@:]) , >:@{:@:])^:([`( ] - {:@[)`(i.@>:@{:@[))
3 7 jose2 10000
8922 111 930 2691 3850 6154 6624 10000
the last number is n, please ignore.
for 3 4 jose2 20,
it iterates 3 4 f (0 1 2 3, 4) 16 times.
where 3 4 = [
16 = ( ] - {:@[))
and 0 1 2 3, 4 = (i.@>:@{:@[)
for the function f, it works like 3 4 f 0 1 2 3, 4 = ((4+1) | 3 + 0 1 2 3),4+1
= 3 0 1 2, 5
and 3 4 f 3 0 1 2, 5 = (6 | 3 + 0 1 2 3),6 and so on.
I am a beginner of J, learned ^: just from this problem. do not know if this
can be simplified.
________________________________
From: Skip Cave <[email protected]>
To: [email protected]
Sent: Saturday, May 4, 2013 1:42 AM
Subject: Re: [Jprogramming] rosettacode
Very nice Elton and Jose! I had a bug in my explicit formula, as well as
having some extra variables I didn't need. So I simplified my formula and
changed the index to the "Mayan Way" (zero index).
jose1=: 4 : 0
k =. y - 1{x NB. k is the number of "kills"
L =. i. y NB. L is the position list
whilst. k =. k-1 do.
L =. (<:0{x) |. L NB. Rotate by the skip number
L =. }. L NB. Remove the dead index.
end.
)
So now my results match Jose and Elton's tacit versions.
3 4 jose1 20
19 1 7 12
5 1 jose1 200
112
7 10 jose1 345
283 287 295 319 72 92 93 137 175 224
Interestingly:
Ts '7 10 jose1 345'
0.0028602 6464
Ts '7 10 jose 345'
0.0010386 65664
Ts '3 7 jose1 10000'
0.0823621 133440
Ts '3 7 jose 10000'
|stack error: jose
| 3 7 jose 10000
Where jose1 is my explicit version, and jose is Elton's tacit version.
Elton, I'm trying to parse through your tacit version to understand how it
works. Could you break it appart, and explain what is going on in there?
Is it possible to implement my explicit algorithm in a tacit expression
that won't get a stack error with large numbers?
Skip
On Fri, May 3, 2013 at 7:13 PM, elton wang <[email protected]> wrote:
> modified from Jose's recursive form:
>
> jose =. i.@{:@[`(] | {.@[ + [ $: _1 + ])@.({:@[ < ])
>
>
--
Skip Cave
Cave Consulting LLC
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm