Re: [Jprogramming] Quora Challenge

2017-09-17 Thread Erling Hellenäs
My first version easily handles 20 000 000. And I thought the second would be faster. Lol. J seems to have a very efficient way to see if a number is a prime. timespacex'2 f 2000' 12.2883 5.36873e8 /Erling On 2017-09-17 16:22, Erling Hellenäs wrote: It takes quite some time at 20

Re: [Jprogramming] Quora Challenge

2017-09-17 Thread Erling Hellenäs
Hi Raul ! This is what happens when I run it in a clean JQT. I added a timespecex call. Not sure what might happen for you. Cheers, Erling Prime=: [: -. [: +./0 = [ | ] (,2) Prime 3 1 NB. 1 2 3 Prime 4 0 NB. 0 2 3 Prime 5 1 NB. 1 2 3 5 Prime 6 0 NB. 0 PrimesUntil=: 3 : 0

Re: [Jprogramming] Quora Challenge

2017-09-17 Thread Erling Hellenäs
It takes quite some time at 20 and more. Due to PrimesUntil, it seems. timespacex'2 f 2000' 0.0121769 42240 timespacex'2 f 2' 0.426833 533760 timespacex'2 f 20' 30.0596 4.20378e6 timespacex'PrimesUntil 20' 30.0592 4.19981e6 /Erling  On 2017-09-17 16:05, Erling Hellenäs

Re: [Jprogramming] Quora Challenge

2017-09-17 Thread Erling Hellenäs
Hi all! Here is a version which does not use the p: verb. It is more useful as description of the algorithm. It is also probably less wasteful of computer resources. Cheers, Erling Prime=: [: -. [: +./0 = [ | ] (,2) Prime 3 NB. 1 2 3 Prime 4 NB. 0 2 3 Prime 5 NB. 1 2 3 5 Prime 6

Re: [Jprogramming] Quora Challenge

2017-09-17 Thread Raul Miller
I was going to look at 200 f 300 and then try timespacex between your f and other implementations of f, but I got a value error on AddStartRangeIfNotThere FYI, -- Raul On Sun, Sep 17, 2017 at 8:23 AM, Erling Hellenäs wrote: > Hi all! > > Here is a version which does

Re: [Jprogramming] Quora Challenge

2017-09-17 Thread Erling Hellenäs
I paste my submission into a new temp, run it in a clean terminal and I see no problems. /Erling On 2017-09-17 17:20, Raul Miller wrote: Ok, thanks - with these additional definitions, your f works for me. Thanks, -- For

Re: [Jprogramming] Quora Challenge

2017-09-17 Thread Raul Miller
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=: [:

Re: [Jprogramming] Quora Challenge

2017-09-17 Thread Erling Hellenäs
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:

Re: [Jprogramming] Quora Challenge

2017-09-17 Thread Skip Cave
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

Re: [Jprogramming] Quora Challenge

2017-09-17 Thread Erling Hellenäs
It failed  on the commented tests, but it's dead fast, yes. /Erling s=: 10 f 100 s >./90 91 92 93 94 95 96 = s NB. s=: 1 f 2 NB. >./s = 1 NB.s=: 2 f 3 NB. 0 = $ s NB. s=: 8 f 9 NB. >./s = 8 9 s=: 7 f 8 >./s = 8 NB.s=: 10 f 11 NB.>./s = 10 s=: 7 f 11 >./s = 8 9 10 s=: 200 f 300 s

Re: [Jprogramming] Quora Challenge

2017-09-17 Thread Erling Hellenäs
"Currently, arguments larger than2^31are tested to be prime according to a probabilistic algorithm (Miller-Rabin)" http://www.jsoftware.com/docs/help804/dictionary/dpco.htm Miller–Rabin primality test https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test This means that sometimes,

Re: [Jprogramming] Quora Challenge

2017-09-17 Thread Raul Miller
Ok, thanks - with these additional definitions, your f works for me. Thanks, -- Raul On Sun, Sep 17, 2017 at 10:05 AM, Erling Hellenäs wrote: > Hi Raul ! > > This is what happens when I run it in a clean JQT. I added a timespecex > call. Not sure what might happen

Re: [Jprogramming] Quora Challenge

2017-09-17 Thread Raul Miller
Oops, biggap=:{~0 1+[:(i.<./)2-/\] Thanks, -- Raul On Sunday, September 17, 2017, Erling Hellenäs 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 >

[Jprogramming] pick up sticks

2017-09-17 Thread Linda Alvord
Try this in JHS on a laptop. load 'plot' f=: 13 :'((<.-:y),2)$(?y#11)j.?y#11' 'pensize 10' plot f 12 g=: 13 :'(y,2)$2#(i.y)j.i.y' 'point;pensize 10' plot g 12 h=: 13 :'((y#0)j.i.y),.(y#<:y)j.i.y' 'pensize 10' plot h 6 v=: 13 :'((i.y)j.y#0),.(i.y)j.y#<:y' 'pensize 10' plot v 6 'pensize 10' plot (h

Re: [Jprogramming] Quora Challenge

2017-09-17 Thread Roger Hui
​Since the Quora Challenge is a question on intervals it seems some sort of sieve calculation would be most efficient, because sieving means you don't have do start from scratch for each number in the interval. About 8 years ago, I designed and implemented a model of p: in Dyalog APL, coded

Re: [Jprogramming] Quora Challenge

2017-09-17 Thread Roger Hui
In theory Miller-Rabin can give incorrect result, but the probability of that is lower than the probability of a cosmic ray particle impacting a circuit in your CPU and causing it to give a wrong answer. I have some thoughts on solving this Quora Challenge which I will post in a bit. On Sun,

Re: [Jprogramming] Quora Challenge

2017-09-17 Thread Roger Hui
> I have some thoughts on solving this Quora Challenge which I will post in a bit. First, a teaser, computed completely in Dyalog APL with no additional C coding. t←(1e6+2e9) pc 2e6+2e9 ⍴t 185 5↑t 2001401814 2001401815 2001401816 2001401817 2001401818 ¯5↑t 2001401994

[Jprogramming] Pick up sticks

2017-09-17 Thread Linda Alvord
Try this in JHS on a laptop. load 'plot' f=: 13 :'((<.-:y),2)$(?y#11)j.?y#11' 'pensize 10' plot f 12 g=: 13 :'(y,2)$2#(i.y)j.i.y' 'point;pensize 10' plot g 12 h=: 13 :'((y#0)j.i.y),.(y#<:y)j.i.y' 'pensize 10' plot h 6 v=: 13 :'((i.y)j.y#0),.(i.y)j.y#<:y' 'pensize 10' plot v 6 'pensize 10' plot (h

[Jprogramming] pick up sticks

2017-09-17 Thread Linda Alvord
Try this in JHS on a laptop. load 'plot' f=: 13 :'((<.-:y),2)$(?y#11)j.?y#11' 'pensize 10' plot f 12 g=: 13 :'(y,2)$2#(i.y)j.i.y' 'point;pensize 10' plot g 12 h=: 13 :'((y#0)j.i.y),.(y#<:y)j.i.y' 'pensize 10' plot h 6 v=: 13 :'((i.y)j.y#0),.(i.y)j.y#<:y' 'pensize 10' plot v 6 'pensize 10' plot (h

[Jprogramming] Pick up sticks

2017-09-17 Thread Linda Alvord
Try this in JHS on a laptop. load 'plot' f=: 13 :'((<.-:y),2)$(?y#11)j.?y#11' 'pensize 10' plot f 12 g=: 13 :'(y,2)$2#(i.y)j.i.y' 'point;pensize 10' plot g 12 h=: 13 :'((y#0)j.i.y),.(y#<:y)j.i.y' 'pensize 10' plot h 6 v=: 13 :'((i.y)j.y#0),.(i.y)j.y#<:y' 'pensize 10' plot v 6 'pensize 10' plot (h