Twin primes: Difference between revisions
Content added Content deleted
(Added Sidef) |
m (→straight-forward prime generator: optimized the GENP function.) |
||
Line 1,097: | Line 1,097: | ||
commas: parse arg _; do ?=length(_)-3 to 1 by -3; _=insert(',', _, ?); end; return _ |
commas: parse arg _; do ?=length(_)-3 to 1 by -3; _=insert(',', _, ?); end; return _ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
genP: parse arg y; |
genP: parse arg y; @.1=2; @.2=3; @.3=5; @.4=7; @.5=11; @.6=13; #= 6; tp= 3; sq.6= 169 |
||
do j= |
do j=@.#+2 by 2 while j<y /*continue on with the next odd prime. */ |
||
parse var j '' -1 _ |
parse var j '' -1 _ /*obtain the last digit of the J var.*/ |
||
if |
if _==5 then iterate; if j// 3==0 then iterate /*J ÷ by 5? J ÷ by 3?*/ |
||
if j |
if j//7==0 then iterate; if j//11==0 then iterate /*" " " 7? " " " 11?*/ |
||
if j // 7 ==0 then iterate /* " " " " " " seven? */ |
|||
if j //11 ==0 then iterate /* " " " " " " eleven?*/ |
|||
/* [↓] divide by the primes. ___ */ |
/* [↓] divide by the primes. ___ */ |
||
do k=6 to # while |
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 ___ */ |
if j//@.k == 0 then iterate j /*÷ by prev. prime? ¬prime ___ */ |
||
end /*k*/ /* [↑] only divide up to √ J */ |
end /*k*/ /* [↑] only divide up to √ J */ |
||
prev= @.#; |
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.*/ |
if j-2==prev then tp= tp + 1 /*This & previous prime twins? Bump TP.*/ |
||
end /*j*/ |
end /*j*/; return tp</lang> |
||
return tp</lang> |
|||
{{out|output|text= when using the default inputs:}} |
{{out|output|text= when using the default inputs:}} |
||
<pre> |
<pre> |