Twin primes: Difference between revisions

m
→‎straight-forward prime generator: optimized the GENP function.
(Added Sidef)
m (→‎straight-forward prime generator: optimized the GENP function.)
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; #= 56; tp= 3; tpsq.6= 2169
do j=13@.#+2 by 2 while j<y /*continue on with the next odd prime. */
parse var j '' -1 _ /*obtain the last digit of the J var.*/
if _ _==5 then iterate; if j// 3==0 then iterate /*isJ this÷ integerby a5? multipleJ of÷ five?by 3?*/
if j // 3 7==0 then iterate; if j//11==0 then iterate /* " " " 7? " " " three11? */
if j // 7 ==0 then iterate /* " " " " " " seven? */
if j //11 ==0 then iterate /* " " " " " " eleven?*/
/* [↓] divide by the primes. ___ */
do k=6 to # while k*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>
return tp</lang>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>