Special neighbor primes: Difference between revisions

Content added Content deleted
(Add Factor)
(→‎{{header|REXX}}: changed program to use neighbor primes.)
Line 198: Line 198:


=={{header|REXX}}==
=={{header|REXX}}==
<lang rexx>/*REXX program finds neighbor primes: P1, P2, P1+P2-1 are primes, and P1 and P2 < 100*/
The output list is displayed in numerical order by prime &nbsp; '''P''' &nbsp; and then by prime &nbsp; '''Q'''.
<lang rexx>/*REXX program finds special neighbor primes: P, Q, P+Q-1 are primes, and P and Q < 100.*/
parse arg hi cols . /*obtain optional argument from the CL.*/
parse arg hi cols . /*obtain optional argument from the CL.*/
if hi=='' | hi=="," then hi= 100 /*Not specified? Then use the default.*/
if hi=='' | hi=="," then hi= 100 /*Not specified? Then use the default.*/
if cols=='' | cols=="," then cols= 5 /* " " " " " " */
if cols=='' | cols=="," then cols= 5 /* " " " " " " */
call genP hi /*build semaphore array for low primes.*/
call genP hi /*build semaphore array for low primes.*/
do p=1 while @.p<hi
low#= #; #m= # - 1 /*obtain the two high primes generated.*/
call genP @.low# + @.#m - 1 /*build semaphore array for high primes*/
end /*p*/; lim= p-1; q= p+1 /*set LIM to prime for P; calc. 2nd HI.*/
#m= # - 1
call genP @.# + @.#m - 1 /*build semaphore array for high primes*/
w= 20 /*width of a number in any column. */
w= 20 /*width of a number in any column. */
title= ' special neighbor primes: P, Q, P+Q-1 are primes, and P and Q < ' commas(hi)
title= ' special neighbor primes: P1, P2, P1+P2-1 are primes, and P1 and P2 < ' ,
commas(hi)
if cols>0 then say ' index │'center(title, 1 + cols*(w+1) )
if cols>0 then say '───────┼'center("" , 1 + cols*(w+1), '─')
if cols>0 then say ' index │'center(title, 1 + cols*(w+1) )
if cols>0 then say '───────┼'center("" , 1 + cols*(w+1), '─')
found= 0; idx= 1 /*init. # special neighbor primes & IDX*/
$= /*a list of sp neighbor primes (so far)*/
found= 0; idx= 1 /*initialize # neighbor primes & index.*/
do j=1 for low#; p= @.j /*look for special neighbor P in range.*/
$= /*a list of neighbor primes (so far).*/
do k=j+1 to low#; q= @.k /* " " " " Q " " */
do j=1 to lim; jp= j+1; q= @.jp /*look for neighbor primes within range*/
s= p+q - 1; if \!.s then iterate /*sum of 2 primes minus one not prime? */
y= @.j + q - 1; if \!.y then iterate /*is X also a prime? No, then skip it.*/
found= found + 1 /*bump number of sp. neighbor primes. */
found= found + 1 /*bump the number of neighbor primes. */
if cols==0 then iterate /*Build the list (to be shown later)? */
if cols==0 then iterate /*Build the list (to be shown later)? */
y= p','q"──►"s /*flag sum-1 is a sp. neighbor prime.*/
$= $ right( @.j','q"──►"y, w) /*add neighbor prime ──► the $ list. */
$= $ right(y, w) /*add sp. neighbor prime ──► the $ list*/
if found//cols\==0 then iterate /*have we populated a line of output? */
if found//cols\==0 then iterate /*have we populated a line of output? */
say center(idx, 7)'│' substr($, 2); $= /*display what we have so far (cols). */
say center(idx, 7)'│' substr($, 2); $= /*display what we have so far (cols). */
idx= idx + cols /*bump the index count for the output*/
end /*j*/
idx= idx + cols /*bump the index count for the output*/
end /*k*/
end /*j*/


if $\=='' then say center(idx, 7)"│" substr($, 2) /*possible display residual output.*/
if $\=='' then say center(idx, 7)"│" substr($, 2) /*possible display residual output.*/
if cols>0 then say '───────┴'center("" , 1 + cols*(w+1), '─')
if cols>0 then say '───────┴'center("" , 1 + cols*(w+1), '─')
say
say
say 'Found ' commas(found) title
say 'Found ' commas(found) title
Line 236: Line 235:
@.1=2; @.2=3; @.3=5; @.4=7; @.5=11 /*define some low primes. */
@.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. */
!.2=1; !.3=1; !.5=1; !.7=1; !.11=1 /* " " " " flags. */
#=5; sq.#= @.# **2 /*number of primes so far; prime square*/
#=5; s.#= @.# **2 /*number of primes so far; prime². */
/* [↓] generate more primes ≤ high.*/
/* [↓] generate more primes ≤ high.*/
do j=@.#+2 by 2 to limit /*find odd primes from here on. */
do j=@.#+2 by 2 to limit /*find odd primes from here on. */
parse var j '' -1 _; if _==5 then iterate /*J ÷ by 5? (right digit).*/
parse var j '' -1 _; if _==5 then iterate /*J divisible by 5? (right dig)*/
if j//3==0 then iterate; if j//7==0 then iterate /*" " " 3? Is J ÷ by 7? */
if j// 3==0 then iterate /*" " " 3? */
do k=5 while sq.k<=j /* [↓] divide by the known odd primes.*/
if j// 7==0 then iterate /*" " " 7? */
if j//@.k==0 then iterate j /*Is J ÷ X? Then not prime. ___ */
/* [↑] the above 3 lines saves time.*/
end /*k*/ /* [] only process numbers ≤ J */
do k=5 while s.k<=j /* [] divide by the known odd primes.*/
#= #+1; @.#= j; sq.#= j*j; !.j= 1 /*bump # of Ps; assign next P; P²; P# */
if j // @.k == 0 then iterate j /*Is J ÷ X? Then not prime. ___ */
end /*j*/; return</lang>
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>
{{out|output|text=&nbsp; when using the default inputs:}}
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
<pre>
index │ special neighbor primes: P, Q, P+Q-1 are primes, and P and Q < 100
index │ special neighbor primes: P1, P2, P1+P2-1 are primes, and P1 and P2 < 100
───────┼──────────────────────────────────────────────────────────────────────────────────────────────────────────
───────┼──────────────────────────────────────────────────────────────────────────────────────────────────────────
1 │ 3,5──►7 3,11──►13 3,17──►19 3,29──►31 3,41──►43
1 │ 3,5──►7 5,7──►11 7,11──►17 11,13──►23 13,17──►29
6 │ 3,59──►61 3,71──►73 5,7──►11 5,13──►17 5,19──►23
6 │ 19,23──►41 29,31──►59 31,37──►67 41,43──►83 43,47──►89
11 │ 5,37──►41 5,43──►47 5,67──►71 5,79──►83 5,97──►101
11 │ 61,67──►127 67,71──►137 73,79──►151
16 7,11──►17 7,13──►19 7,17──►23 7,23──►29 7,31──►37
21 │ 7,37──►43 7,41──►47 7,47──►53 7,53──►59 7,61──►67
26 │ 7,67──►73 7,73──►79 7,83──►89 7,97──►103 11,13──►23
31 │ 11,19──►29 11,31──►41 11,37──►47 11,43──►53 11,61──►71
36 │ 11,73──►83 11,79──►89 11,97──►107 13,17──►29 13,19──►31
41 │ 13,29──►41 13,31──►43 13,41──►53 13,47──►59 13,59──►71
46 │ 13,61──►73 13,67──►79 13,71──►83 13,89──►101 13,97──►109
51 │ 17,31──►47 17,37──►53 17,43──►59 17,67──►83 17,73──►89
56 │ 17,97──►113 19,23──►41 19,29──►47 19,41──►59 19,43──►61
61 │ 19,53──►71 19,61──►79 19,71──►89 19,79──►97 19,83──►101
66 │ 19,89──►107 23,31──►53 23,37──►59 23,61──►83 23,67──►89
71 │ 23,79──►101 29,31──►59 29,43──►71 29,61──►89 29,73──►101
76 │ 29,79──►107 31,37──►67 31,41──►71 31,43──►73 31,53──►83
81 │ 31,59──►89 31,67──►97 31,71──►101 31,73──►103 31,79──►109
86 │ 31,83──►113 31,97──►127 37,43──►79 37,47──►83 37,53──►89
91 │ 37,61──►97 37,67──►103 37,71──►107 37,73──►109 41,43──►83
96 │ 41,61──►101 41,67──►107 41,73──►113 41,97──►137 43,47──►89
101 │ 43,59──►101 43,61──►103 43,67──►109 43,71──►113 43,89──►131
106 │ 43,97──►139 47,61──►107 47,67──►113 53,61──►113 53,79──►131
111 │ 53,97──►149 59,73──►131 59,79──►137 61,67──►127 61,71──►131
116 │ 61,79──►139 61,89──►149 61,97──►157 67,71──►137 67,73──►139
121 │ 67,83──►149 67,97──►163 71,79──►149 71,97──►167 73,79──►151
126 │ 79,89──►167 83,97──►179
───────┴──────────────────────────────────────────────────────────────────────────────────────────────────────────
───────┴──────────────────────────────────────────────────────────────────────────────────────────────────────────


Found 127 special neighbor primes: P, Q, P+Q-1 are primes, and P and Q < 100
Found 13 special neighbor primes: P1, P2, P1+P2-1 are primes, and P1 and P2 < 100
</pre>
</pre>