Twin primes: Difference between revisions

m
→‎{{header|REXX}}: optimized the GENP subroutine.
m (→‎{{header|REXX}}: optimized the GENP subroutine.)
Line 1,097:
commas: parse arg _; do ?=length(_)-3 to 1 by -3; _=insert(',', _, ?); end; return _
/*──────────────────────────────────────────────────────────────────────────────────────*/
genP: parse arg y; @.1=2; @.2=3; @.3=5; @.4=7; @.5=11; @.6=13; #= 6; tp= 32; sq.6= 169
if y>10 then tp= tp+1
do j=@.#+2 by 2 while j<y /*continue on with the next odd prime. */
do j=@.#+2 parseby var2 j ''for max(0, y%2-@.#%2-1 _ ) /*obtainfind theodd lastprimes digitfrom ofhere theon. J var.*/
parse var ifj '' -1 _==5 then iterate; if j// 3==0 then iterate /*Jobtain ÷the bylast 5?digit Jof ÷the by J 3?var.*/
if if j//7_==05 then iterate; if j//11 3==0 then iterate /*"J "÷ by " 75? " " J "÷ 11by 3? */
if j//7==0 then iterate; if j//11==0 then iterate /*" " " 7? " " " 11? */
/* [↓] divide by the primes. ___ */
do k=6 to # while sq.k<=j /*divide J by other primes ≤ √ J */
if j//@.k == 0 then iterate j /*÷ by prev. prime? ¬prime ___ */
end /*k*/ /* [↑] only divide up to √ J */
prev= @.#; #= #+1; sq.#= j*j; @.#= j /*save prev. P; bump # primes; assign P*/
if j-2==prev then tp= tp + 1 /*This & previous prime twins? Bump TP.*/
end /*j*/; return tp</lang>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 1,125 ⟶ 1,126:
This version won't return a correct value (for the number of twin pairs) for a limit < 73 &nbsp; (because of the manner in
<br>which low primes are generated from a list), &nbsp; however, &nbsp; the primes are returned from the function.
<lang rexx>/*REXX pgm counts the number of twin prime pairs under a specified number N (or a list).*/
parse arg $ . /*get optional number of primes to find*/
if $='' | $="," then $= 100 1000 10000 100000 1000000 10000000 /*No $? Use default.*/
Line 1,139 ⟶ 1,140:
/*──────────────────────────────────────────────────────────────────────────────────────*/
genP: arg y; _= 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101
tp=8; tp=8; #= words(_); sq.103=103*103 /*#: number of prims; TP: # twin pairs.*/
do aa=1 for #; @.aa= word(_, aa) /*assign some low primes for quick ÷'s.*/
end /*aa*/
Line 1,152 ⟶ 1,153:
end /*a*/
/* [↓] divide by the primes. ___ */
do k=27 to # while sq.k*k <= j /*divide J by other primes ≤ √ J */
if j//@.k ==0 then iterate j /*÷ by prev. prime? ¬prime ___ */
end /*k*/ /* [↑] only divide up to √ J */
prev= @.#; #= #+1; sq.#= j*j; @.#= j /*save prev. P; bump # primes; assign P*/
if j-2==prev then tp= tp + 1 /*This & previous prime twins? Bump TP.*/
end /*j*/; return tp</lang><br><br>
return tp</lang><br><br>
 
=={{header|Ring}}==