Strange unique prime triplets: Difference between revisions
m (→{{header|Phix}}: added get_primes_le(30) and (1000) comment) |
m (→{{header|REXX}}: added wordage to the output texts to show more information.) |
||
Line 178: | Line 178: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
<lang rexx>/*REXX pgm lists |
<lang rexx>/*REXX pgm lists triplet strange primes (<HI) where the sum of the primes sum is a prime*/ |
||
parse arg hi . /*obtain optional argument from the CL.*/ |
parse arg hi . /*obtain optional argument from the CL.*/ |
||
if hi=='' | hi=="," then hi= 30 /*Not specified? Then use the default.*/ |
if hi=='' | hi=="," then hi= 30 /*Not specified? Then use the default.*/ |
||
tell= hi>0; hi= abs(hi); hi= hi - 1 /*use the absolute value of HI. */ |
tell= hi>0; hi= abs(hi); hi= hi - 1 /*use the absolute value of HI. */ |
||
if tell>0 then say 'list of unique |
if tell>0 then say 'list of unique triplet strange primes that sum to a prime:' |
||
call genP /*build array of semaphores for primes.*/ |
call genP /*build array of semaphores for primes.*/ |
||
finds= 0 /*The number of strange primes (so far)*/ |
finds= 0 /*The number of strange primes (so far)*/ |
||
say |
say |
||
do m=2+1 by 2 to hi; if \!.m then iterate |
do m=2+1 by 2 to hi; if \!.m then iterate /*just use the odd primes. */ |
||
do n=m+2 by 2 to hi; if \!.n then iterate |
do n=m+2 by 2 to hi; if \!.n then iterate /* " " " " " */ |
||
mn= m + n |
mn= m + n /*partial sum (deep loops).*/ |
||
do p=n+2 by 2 to hi; if \!.p then iterate |
do p=n+2 by 2 to hi; if \!.p then iterate /*just use the odd primes. */ |
||
sum= mn + p /*compute sum of 3 primes. */ |
sum= mn + p /*compute sum of 3 primes. */ |
||
if \!.sum then iterate /*Is the sum a prime? No, skip it |
if \!.sum then iterate /*Is the sum a prime? No, then skip it*/ |
||
finds= finds + 1 /*bump |
finds= finds + 1 /*bump # of triple "strange" primes. */ |
||
if tell then say right(m, w+9) right(n, w) right(p, w) , |
if tell then say right(m, w+9) right(n, w) right(p, w) , |
||
' sum to: ' right(sum, w) |
' sum to: ' right(sum, w) |
||
Line 199: | Line 199: | ||
end /*m*/ |
end /*m*/ |
||
say |
say |
||
say 'Found ' |
say 'Found ' commas(finds) " unique triplet strange primes which sum to a prime," , |
||
" each triplet numbers are < " commas(hi+1). |
|||
exit 0 /*stick a fork in it, we're all done. */ |
exit 0 /*stick a fork in it, we're all done. */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
Line 219: | Line 219: | ||
end /*k*/ /* [↑] only process numbers ≤ √ J */ |
end /*k*/ /* [↑] only process numbers ≤ √ J */ |
||
#= #+1; @.#= j; s.#= j*j; !.j= 1 /*bump # of Ps; assign next P; P²; P# */ |
#= #+1; @.#= j; s.#= j*j; !.j= 1 /*bump # of Ps; assign next P; P²; P# */ |
||
end /*j*/ |
end /*j*/; return</lang> |
||
return</lang> |
|||
{{out|output|text= when using the default input:}} |
{{out|output|text= when using the default input:}} |
||
<pre> |
<pre> |
||
list of unique |
list of unique triplet strange primes that sum to a prime: |
||
3 5 11 sum to: 19 |
3 5 11 sum to: 19 |
||
Line 268: | Line 267: | ||
19 23 29 sum to: 71 |
19 23 29 sum to: 71 |
||
Found 42 unique strange primes which sum to a prime, each |
Found 42 unique triplet strange primes which sum to a prime, each triplet numbers are < 30. |
||
</pre> |
</pre> |
||
{{out|output|text= when using the input of: <tt> -1000 </tt>}} |
{{out|output|text= when using the input of: <tt> -1000 </tt>}} |
||
<pre> |
<pre> |
||
Found 241,580 unique strange primes which sum to a prime, each |
Found 241,580 unique triplet strange primes which sum to a prime, each triplet numbers are < 1,000. |
||
</pre> |
</pre> |
||
Revision as of 14:37, 10 March 2021
Integers n, m, and p are strange unique primes if n, m, and p are unique primes, and the sum of n + m + p is also prime.
- Task
-
- Find all triplets of strange unique primes in which n, m, and p are all less than 30.
- (stretch goal) Show the count (only) of all the triplets of strange unique primes in which n, m, and p are all less than 1,000.
Factor
<lang factor>USING: formatting io kernel math math.combinatorics math.primes sequences tools.memory.private ;
- strange ( n -- seq )
primes-upto 3 [ sum prime? ] filter-combinations ;
30 strange [
1 + over sum [ prefix ] dip suffix "%2d: %2d+%2d+%2d = %d\n" vprintf
] each-index nl
1,000 strange length commas "Found %s strange prime triplets with n, m, p < 1,000.\n" printf</lang>
- Output:
1: 3+ 5+11 = 19 2: 3+ 5+23 = 31 3: 3+ 5+29 = 37 4: 3+ 7+13 = 23 5: 3+ 7+19 = 29 6: 3+11+17 = 31 7: 3+11+23 = 37 8: 3+11+29 = 43 9: 3+17+23 = 43 10: 5+ 7+11 = 23 11: 5+ 7+17 = 29 12: 5+ 7+19 = 31 13: 5+ 7+29 = 41 14: 5+11+13 = 29 15: 5+13+19 = 37 16: 5+13+23 = 41 17: 5+13+29 = 47 18: 5+17+19 = 41 19: 5+19+23 = 47 20: 5+19+29 = 53 21: 7+11+13 = 31 22: 7+11+19 = 37 23: 7+11+23 = 41 24: 7+11+29 = 47 25: 7+13+17 = 37 26: 7+13+23 = 43 27: 7+17+19 = 43 28: 7+17+23 = 47 29: 7+17+29 = 53 30: 7+23+29 = 59 31: 11+13+17 = 41 32: 11+13+19 = 43 33: 11+13+23 = 47 34: 11+13+29 = 53 35: 11+17+19 = 47 36: 11+19+23 = 53 37: 11+19+29 = 59 38: 13+17+23 = 53 39: 13+17+29 = 59 40: 13+19+29 = 61 41: 17+19+23 = 59 42: 19+23+29 = 71 Found 241,580 strange prime triplets with n, m, p < 1,000.
Phix
<lang Phix>requires("0.8.4") constant primes = get_primes_le(30) --constant primes = get_primes(-10) -- earlier versions
procedure strange_triplets()
-- -- It is not worth involving 2, ie primes[1], -- since (2 + any other two primes) is even, -- also we may as well leave space for {j,k}, -- {k} in the two outer loops -- sequence res = {} for i=2 to length(primes)-2 do integer n = primes[i] for j=i+1 to length(primes)-1 do integer m = primes[j] for k=j+1 to length(primes) do integer p = primes[k], nmp = n+m+p if is_prime(nmp) then res = append(res,sprintf("%2d: %2d+%2d+%2d = %d", {length(res)+1, n, m, p, nmp})) end if end for end for end for printf(1,"%d found:\n%s\n",{length(res),join(shorten(res,"",3),"\n")})
end procedure
strange_triplets()</lang>
- Output:
42 found: 1: 3+ 5+11 = 19 2: 3+ 5+23 = 31 3: 3+ 5+29 = 37 ... 40: 13+19+29 = 61 41: 17+19+23 = 59 42: 19+23+29 = 71
With get_primes_le(1000), or get_primes(-168), it finds 241,580 triplets.
Python
Using sympy.primerange.
<lang python>from sympy import primerange
def strange_triplets(mx: int = 30) -> None:
primes = list(primerange(0, mx)) primes3 = set(primerange(0, 3 * mx)) c = 0 for i, n in enumerate(primes): for j, m in enumerate(primes[i + 1:], i + 1): for p in primes[j + 1:]: if n + m + p in primes3: c += 1 print(f"{c:2}: {n:2}+{m:2}+{p:2} = {n + m + p}")
strange_triplets()</lang>
- Output:
1: 3+ 5+11 = 19 2: 3+ 5+23 = 31 3: 3+ 5+29 = 37 4: 3+ 7+13 = 23 5: 3+ 7+19 = 29 6: 3+11+17 = 31 7: 3+11+23 = 37 8: 3+11+29 = 43 9: 3+17+23 = 43 10: 5+ 7+11 = 23 11: 5+ 7+17 = 29 12: 5+ 7+19 = 31 13: 5+ 7+29 = 41 14: 5+11+13 = 29 15: 5+13+19 = 37 16: 5+13+23 = 41 17: 5+13+29 = 47 18: 5+17+19 = 41 19: 5+19+23 = 47 20: 5+19+29 = 53 21: 7+11+13 = 31 22: 7+11+19 = 37 23: 7+11+23 = 41 24: 7+11+29 = 47 25: 7+13+17 = 37 26: 7+13+23 = 43 27: 7+17+19 = 43 28: 7+17+23 = 47 29: 7+17+29 = 53 30: 7+23+29 = 59 31: 11+13+17 = 41 32: 11+13+19 = 43 33: 11+13+23 = 47 34: 11+13+29 = 53 35: 11+17+19 = 47 36: 11+19+23 = 53 37: 11+19+29 = 59 38: 13+17+23 = 53 39: 13+17+29 = 59 40: 13+19+29 = 61 41: 17+19+23 = 59 42: 19+23+29 = 71
REXX
<lang rexx>/*REXX pgm lists triplet strange primes (<HI) where the sum of the primes sum is a prime*/ parse arg hi . /*obtain optional argument from the CL.*/ if hi== | hi=="," then hi= 30 /*Not specified? Then use the default.*/ tell= hi>0; hi= abs(hi); hi= hi - 1 /*use the absolute value of HI. */ if tell>0 then say 'list of unique triplet strange primes that sum to a prime:' call genP /*build array of semaphores for primes.*/ finds= 0 /*The number of strange primes (so far)*/ say
do m=2+1 by 2 to hi; if \!.m then iterate /*just use the odd primes. */ do n=m+2 by 2 to hi; if \!.n then iterate /* " " " " " */ mn= m + n /*partial sum (deep loops).*/ do p=n+2 by 2 to hi; if \!.p then iterate /*just use the odd primes. */ sum= mn + p /*compute sum of 3 primes. */ if \!.sum then iterate /*Is the sum a prime? No, then skip it*/ finds= finds + 1 /*bump # of triple "strange" primes. */ if tell then say right(m, w+9) right(n, w) right(p, w) , ' sum to: ' right(sum, w) end /*p*/ end /*n*/ end /*m*/
say say 'Found ' commas(finds) " unique triplet strange primes which sum to a prime," ,
" each triplet numbers are < " commas(hi+1).
exit 0 /*stick a fork in it, we're all done. */ /*──────────────────────────────────────────────────────────────────────────────────────*/ commas: parse arg ?; do jc=length(?)-3 to 1 by -3; ?=insert(',', ?, jc); end; return ? /*──────────────────────────────────────────────────────────────────────────────────────*/ genP: !.= 0; w= length(hi) /*placeholders for primes; width of #'s*/
@.1=2; @.2=3; @.3=5; @.4=7; @.5=11 /*define some low primes. */ !.2=1; !.3=1; !.5=1; !.7=1; !.11=1 /* " " " " flags. */ #=5; s.#= @.# **2 /*number of primes so far; prime². */ /* [↓] generate more primes ≤ high.*/ do j=@.#+2 by 2 for hi*3%2 /*find odd primes from here on. */ parse var j -1 _; if _==5 then iterate /*J divisible by 5? (right dig)*/ if j// 3==0 then iterate /*" " " 3? */ if j// 7==0 then iterate /*" " " 7? */ /* [↑] the above five lines saves time*/ do k=5 while s.k<=j /* [↓] divide by the known odd primes.*/ if j // @.k == 0 then iterate j /*Is J ÷ X? Then not prime. ___ */ end /*k*/ /* [↑] only process numbers ≤ √ J */ #= #+1; @.#= j; s.#= j*j; !.j= 1 /*bump # of Ps; assign next P; P²; P# */ end /*j*/; return</lang>
- output when using the default input:
list of unique triplet strange primes that sum to a prime: 3 5 11 sum to: 19 3 5 23 sum to: 31 3 5 29 sum to: 37 3 7 13 sum to: 23 3 7 19 sum to: 29 3 11 17 sum to: 31 3 11 23 sum to: 37 3 11 29 sum to: 43 3 17 23 sum to: 43 5 7 11 sum to: 23 5 7 17 sum to: 29 5 7 19 sum to: 31 5 7 29 sum to: 41 5 11 13 sum to: 29 5 13 19 sum to: 37 5 13 23 sum to: 41 5 13 29 sum to: 47 5 17 19 sum to: 41 5 19 23 sum to: 47 5 19 29 sum to: 53 7 11 13 sum to: 31 7 11 19 sum to: 37 7 11 23 sum to: 41 7 11 29 sum to: 47 7 13 17 sum to: 37 7 13 23 sum to: 43 7 17 19 sum to: 43 7 17 23 sum to: 47 7 17 29 sum to: 53 7 23 29 sum to: 59 11 13 17 sum to: 41 11 13 19 sum to: 43 11 13 23 sum to: 47 11 13 29 sum to: 53 11 17 19 sum to: 47 11 19 23 sum to: 53 11 19 29 sum to: 59 13 17 23 sum to: 53 13 17 29 sum to: 59 13 19 29 sum to: 61 17 19 23 sum to: 59 19 23 29 sum to: 71 Found 42 unique triplet strange primes which sum to a prime, each triplet numbers are < 30.
- output when using the input of: -1000
Found 241,580 unique triplet strange primes which sum to a prime, each triplet numbers are < 1,000.
Ring
<lang ring> load "stdlib.ring"
num = 0 limit = 30
see "working..." + nl see "the strange primes are:" + nl
for n = 1 to limit
for m = n+1 to limit for p = m+1 to limit sum = n+m+p if isprime(sum) and isprime(n) and isprime(m) and isprime(p) num = num + 1 see "" + num + ": " + n + "+" + m + "+" + p + " = " + sum + nl ok next next
next
see "done..." + nl </lang>
- Output:
working... the strange primes are: 1: 3+5+11 = 19 2: 3+5+23 = 31 3: 3+5+29 = 37 4: 3+7+13 = 23 5: 3+7+19 = 29 6: 3+11+17 = 31 7: 3+11+23 = 37 8: 3+11+29 = 43 9: 3+17+23 = 43 10: 5+7+11 = 23 11: 5+7+17 = 29 12: 5+7+19 = 31 13: 5+7+29 = 41 14: 5+11+13 = 29 15: 5+13+19 = 37 16: 5+13+23 = 41 17: 5+13+29 = 47 18: 5+17+19 = 41 19: 5+19+23 = 47 20: 5+19+29 = 53 21: 7+11+13 = 31 22: 7+11+19 = 37 23: 7+11+23 = 41 24: 7+11+29 = 47 25: 7+13+17 = 37 26: 7+13+23 = 43 27: 7+17+19 = 43 28: 7+17+23 = 47 29: 7+17+29 = 53 30: 7+23+29 = 59 31: 11+13+17 = 41 32: 11+13+19 = 43 33: 11+13+23 = 47 34: 11+13+29 = 53 35: 11+17+19 = 47 36: 11+19+23 = 53 37: 11+19+29 = 59 38: 13+17+23 = 53 39: 13+17+29 = 59 40: 13+19+29 = 61 41: 17+19+23 = 59 42: 19+23+29 = 71 done...