Twin primes: Difference between revisions

3,708 bytes removed ,  3 years ago
m
→‎{{header|REXX}}: optimized the GENP function, elided "not equal" compare.
m (inserted a line break.)
m (→‎{{header|REXX}}: optimized the GENP function, elided "not equal" compare.)
Line 552:
=={{header|REXX}}==
The   '''genP'''   function could be optimized for higher specifications of the limit(s).
<!--
=== number of twin primes ===
Note that this REXX program version counts the number of &nbsp; '''twin primes''', &nbsp; not the number of &nbsp; '''twin pairs'''.
<lang rexx>/*REXX program counts the number of twin primes under a specified number N (or a list).*/
parse arg $ . /*get optional number of primes to find*/
if $='' | $="," then $= 100 1000 10000 100000 /*Not specified? Then assume default.*/
w= length( commas( word($, words($) ) ) ) /*get length of the last number in list*/
@found= ' twin primes found under ' /*literal used in the showing of output*/
 
do i=1 for words($); x= word($, i) /*process each N─limit in the $ list.*/
say right( commas(genP(x)), 20) @found right(commas(x), max(length(x), w) )
end /*i*/
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
commas: parse arg _; do ?=length(_)-3 to 1 by -3; _=insert(',', _, ?); end; return _
/*──────────────────────────────────────────────────────────────────────────────────────*/
genP: arg y; @.1=2; @.2=3; @.3=5; @.4=7; @.5=11; @.6=13; #= 5; tp= 3; s= @.# + 2
do j=s by 2 while j<y+2 /*continue on with the next odd prime. */
parse var j '' -1 _ /*obtain the last digit of the J var.*/
if _ ==5 then iterate /*is this integer a multiple of five? */
if j // 3 ==0 then iterate /* " " " " " " three? */
if j // 7 ==0 then iterate /* " " " " " " seven? */
if j //11 ==0 then iterate /* " " " " " " eleven?*/
/* [↓] divide by the primes. ___ */
do k=6 to # while 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 */
#= #+1 /*bump the count of number of primes. */
@.#= j; _= # - 1 /*define J prime; point to prev. prime.*/
if j-2\==@._ then iterate /*This & previous prime not twins? Skip*/
if j-2<y then tp= tp + 1 /*Is this part of the twin pair? Bump.*/
if j <y then tp= tp + 1 /* " " " " " " " " */
end /*j*/
return tp</lang>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
15 twin primes found under 100
69 twin primes found under 1,000
409 twin primes found under 10,000
2,447 twin primes found under 100,000
</pre>
 
=== number of twin prime pairs ===
Note that this REXX program version counts the number of &nbsp; '''twin prime pairs''', &nbsp; not the number of &nbsp; '''twin primes'''.
 
!-->
<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*/
Line 622 ⟶ 576:
if j//@.k == 0 then iterate j /*÷ by prev. prime? ¬prime ___ */
end /*k*/ /* [↑] only divide up to √ J */
#prev= @.#+1 ; #= # + 1; @.#= j /*bumpsave theprev. countP; ofbump number of# primes.; assign P*/
@.#=if j;-2==prev then _tp= #tp -+ 1 /*defineThis J& previous prime; pointtwins? toBump prev. primeTP.*/
if j-2\==@._ then iterate end /*This & previous prime not twins? Skipj*/
tp= tp + 1 /*bump count of twin pairs (both are<Y)*/
end /*j*/
return tp</lang>
{{out|output|text=&nbsp; when using the default inputs:}}