Strange things happen here. It works but give incorrect results. I just
changed F to lower case to put it through my tests. Not sure what might
happen here. I have J 8.04 on this machine. 64 bit.
I never doubted that p: was good. I just thought that since it was a
Quora challenge that using p: would not be a good idea, since the
algorithm used in p: is not known to the audience. It also seemed like
some kind of cheating, like using a subroutine someone else wrote to
solve half the problem.
Well, I also thought p: would have to generate the array of primes for
each call, but it's obviously not the case. There is a formula which
does the trick? Or a hash table of primes?
thru=: <. + >:@>. i.@- <.
biggap=: {~ 0 1 - [: (i. <./) 2 -/\ ]
f=: [: thru/ 1 _1 + [: biggap <./ >. >./ <. thru&.(p:inv)
s=: 10 f 100
s
20 21 22
>./90 91 92 93 94 95 96 = s
|length error: scriptd
| >./90 91 92 93 94 95 96 =s
|[-5] j:\j64-803-user\projects\quoraraul.ijs
inv
^:_1
Cheers,
Erling
On 2017-09-17 17:38, Raul Miller wrote:
Yes. :)
When J provides a mechanism for something it is usually worth trying.
Sometimes you can write something faster, but usually J's approach
will be useful.
For example, in this case, consider this approach:
thru=: <. + >:@>. i.@- <.
biggap=: {~ 0 1 - [: (i. <./) 2 -/\ ]
F=: [: thru/ 1 _1 + [: biggap <./ >. >./ <. thru&.(p:inv)
There will be cases where another approach could be more efficient,
but it performs reasonably well.
Thanks,
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm