The fact that the problem is a Quora challenge is exactly the point. It's like if someone asks your for the square root of 75976307044. Yes, there is a manual process that one can use to compute the square root. But why spend all that time when one can type %:75976307044 or use a hand-held calculator, and get the answer instantly. As an engineer, speed and accuracy are more important than going through a complicated manual process to get that same answer.
I post lots of these J solutions on Quora to remind folks of that fact. It's not that I'm against knowing the manual approach to the solutions. It's just that the more tools we have to make hard problems easier, the better we can solve even harder problems. Skip Cave Cave Consulting LLC On Sun, Sep 17, 2017 at 12:28 PM, Erling Hellenäs <erl...@erlinghellenas.se> wrote: > 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 > ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm