Anonymous user
Twin primes: Difference between revisions
m
→{{header|REXX}}: optimized the GENP subroutine.
m (→optimized prime number generator: fixed a typo.) |
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=
if y>10 then tp= tp+1
do j=@.#+2
parse var
if
if j//7==0 then iterate; if j//11==0 then iterate /*" " " 7? " " " 11? */
/* [↓] divide by the primes. ___ */
{{out|output|text= 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 (because of the manner in
<br>which low primes are generated from a list), however, 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
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
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>
=={{header|Ring}}==
|