Skip to content

Instantly share code, notes, and snippets.

@aks
Created May 12, 2013 09:46
Show Gist options
  • Save aks/5563008 to your computer and use it in GitHub Desktop.
Save aks/5563008 to your computer and use it in GitHub Desktop.
Sum of Two Prime Numbers: If p, q > 2 are consecutive in set of primes. Since p,q can only be odd number, (p+q) is an even number. Can (p+q)/2 be prime? It appears not, as confirmed for the pairs of consecutive primes in first million primes. See the J program below.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment