All the boxing is unnecessary.
Here is the performance you win (j801):
ts'+/>1&p: each 2 %~ each +/ each (2 <\ ])p: i. 1000000'
6.4344903 1.8283457e9
ts' +/1 p:2%~ 2 +/\ p: i. 1000000'
0.38894168 1.9701402e8
Rule of thumb: don't box items of same shape.
R.E. Boss
> -----Oorspronkelijk bericht-----
> Van: [email protected]
> [mailto:[email protected]] Namens Alan Stebbens
> Verzonden: zondag 12 mei 2013 12:08
> Aan: [email protected]
> Onderwerp: [Jprogramming] Testing consecutive pairs of primes
>
> ProgrammingPraxis (at http://programmingpraxis.com/2013/05/10/mindcipher)
> offered a problem asking, given p, q as two consecutive pairs of primes, if
> (p+2)%2 could be prime.
>
> Since both p & q (> 2) are prime, their sum is an even number and not
> prime, but could the half of their sum be a prime?
>
> I'm not much of a mathematician, but I figured I could brute-force an
> approximation with J.
>
> The gist below is my experiment showing that the answer is no, for the
> consecutive pairs of primes in the set of the first million primes. While
> it might be possible for the larger primes, I'm thinking not - just by
> induction.
>
> Probably some of you could show a proof, but it was more fun for me to
> cobble up this in J, and also demonstrate J to the non-J audience at
> Programming Praxis (which has been mostly scheme, Haskell, python, ruby).
>
> https://gist.github.com/aks/5563008
>
> Here's my experiment:
>
> i. 20
> 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
>
> NB. generate the first 20 primes
> p: i. 20
> 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71
>
> NB. box up consecutive pairs of those primes
> (2 <\ ]) p: i. 20
> ┌───┬───┬───┬────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬
> ─────┐
> │2 3│3 5│5 7│7 11│11 13│13 17│17 19│19 23│23 29│29 31│31 37│37 41│41 43│43
> 47│47 53│53 59│59 61│61 67│67 71│
> └───┴───┴───┴────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴
> ─────┘
>
> NB. sum up each pair of primes
> +/ each (2 <\ ])p: i. 20
> ┌─┬─┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┬───┬───┬───┬───┬───┐
> │5│8│12│18│24│30│36│42│52│60│68│78│84│90│100│112│120│128│138│
> └─┴─┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴───┴───┴───┴───┴───┘
>
> NB. divide each sum by 2
> 2 %~ each +/ each (2 <\ ])p: i.
> 20
>
> ┌───┬─┬─┬─┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┐
> │2.5│4│6│9│12│15│18│21│26│30│34│39│42│45│50│56│60│64│69│
> └───┴─┴─┴─┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┘
>
> NB. now, test each of those results for being prime. 1 p: y -- tests y
> for being prime
>
> 1&p: each 2 %~ each +/ each (2 <\ ])p: i.
> 20
>
> ┌─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┐
> │0│0│0│0│0│0│0│0│0│0│0│0│0│0│0│0│0│0│0│
> └─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┘
>
> NB. open the boxed results, so we can add them up
> >1&p: each 2 %~ each +/ each (2 <\ ])p: i. 20
> 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
>
> NB. sum/reduce the vector of booleans. If there's a prime, the sum will
> be > 0
> +/>1&p: each 2 %~ each +/ each (2 <\ ])p: i. 20
> 0
>
> NB. ok. No primes. Let's keep checking for larger groups
>
> +/>1&p: each 2 %~ each +/ each (2 <\ ])p: i. 1000
> 0
> +/>1&p: each 2 %~ each +/ each (2 <\ ])p: i. 10000
> 0
> +/>1&p: each 2 %~ each +/ each (2 <\ ])p: i. 100000
> 0
>
> NB. the previous output took a few seconds. The next will take a few
> minutes
>
> +/>1&p: each 2 %~ each +/ each (2 <\ ])p: i. 1000000
> 0
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm