On Thu, Aug 5, 2010 at 6:29 PM, Dan Bron <[email protected]> wrote:
> Below is a translation as straightforward and mechanical as I could make it.
> It's actually almost line-for-line.
> One difference that may account for the error is that
> the C consumes an integer and a char array, whereas J would balk at doing
> arithmetic on characters, so the inputs are an integer and an integer array.
We're not doing arithmetic on the characters in the array a, except
each time we append a new element to the end. Still, integers might
be the right choice for J.
> Performance is godawful:
The performance of the C function doesn't scale well either. We
should try writing an optimized function at one point.
> Note that the verb has a bug. The results it gives are wrong.
This line:
> while. (-.q)*.(u <= l) do.
it should be this instead:
while. (-.q)*.(u <: l) do.
Also this line (I removed some whitespace):
> q =.(a{~ n - (l * (s - 1)) - u) ~: a{~ (n -(l * s)- u)
should instead say
q =.(a{~ (n - (l * (s - 1))) - u) ~: a{~ ((n - (l * s)) - u)
The whole fixed verb with some further changes can be found below.
This one is now a monad that only takes a single number, eg. use it
like this:
gijt 30
1 1 2 1 1 2 2 2 3 1 1 2 1 1 2 2 2 3 2 1 1 2 1 1 2 2 2 3 1 1
Ambrus
gijt =: 3 :0
nm =. y
a =. $0
for_n. i. nm do.
r =. 1
for_l. 1 +i.<.-:n do.
s =. 0
q =. 0
while. -.q do.
s =. >:s
if. (l * (1+s)) <: n do.
u=.1
while. (-.q)*.(u <: l) do.
q =.(a{~ n-u+l*<:s) ~: a{~ n-u+l*s
u =. >: u
end.
else.
q =. 1
end.
end.
if. r < s do.
r =. s
end.
end.
a =. a , r
end.
NB.}
)
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm