Erdős-primes: Difference between revisions
Content deleted Content added
m →{{header|REXX}}: added a stub. |
→{{header|REXX}}: added the computer programming language REXX. |
||
Line 10: | Line 10: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
<lang rexx>/*REXX program counts and shows the number of Erdős primes under a specified number N. */ |
|||
<lang rexx></lang> |
|||
parse arg n cols . /*get optional number of primes to find*/ |
|||
if n=='' | n=="," then n= 500 /*Not specified? Then assume default.*/ |
|||
if cols=='' | cols=="," then cols= 10 /* " " " " " .*/ |
|||
Ocols= cols; cols= abs(cols) /*Use the absolute value of cols. */ |
|||
call genP n /*generate all primes under N. */ |
|||
primes= 0 /*initialize the number of Erdős primes*/ |
|||
$= /*a list of Erdős primes (so far). */ |
|||
do j=1 until j>=n /*lets now search for Erdős primes. */ |
|||
if \!.j then iterate /*Is J not a prime? Then skip it. */ |
|||
_= sumDigs(j); if \!._ then iterate /*Is sum of J's digs a prime? No, skip.*/ |
|||
primes= primes + 1 /*bump the count of Erdős primes. */ |
|||
if Ocols<1 then iterate /*Build the list (to be shown later)? */ |
|||
$= $ right(j, w) /*add the Erdős prime to the $ list. */ |
|||
if primes//cols\==0 then iterate /*have we populated a line of output? */ |
|||
say substr($, 2); $= /*display what we have so far (cols). */ |
|||
end /*j*/ |
|||
if $\=='' then say substr($, 2) /*possible display some residual output*/ |
|||
say |
|||
say 'found ' primes " Erdős primes < " n |
|||
exit 0 /*stick a fork in it, we're all done. */ |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
sumDigs: parse arg x 1 s 2; do k=2 for length(x)-1; s= s + substr(x,k,1); end; return s |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
genP: parse arg n; @.=.; @.1=2; @.2=3; @.3=5; @.4=7; @.5=11; @.6=13; @.7=17; #= 7 |
|||
w= length(n); !.=0; !.2=1; !.3=1; !.5=1; !.7=1; !.11=1; !.13=1; !.17=1 |
|||
do j=@.7+2 by 2 while j<n /*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? */ |
|||
/* [↓] divide by the primes. ___ */ |
|||
do k=4 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; @.#= j; !.j= 1 /*bump prime count; assign prime & flag*/ |
|||
end /*j*/ |
|||
return</lang> |
|||
{{out|output|text= when using the default inputs:}} |
|||
<pre> |
|||
2 3 5 7 11 23 29 41 43 47 |
|||
61 67 83 89 101 113 131 137 139 151 |
|||
157 173 179 191 193 197 199 223 227 229 |
|||
241 263 269 281 283 311 313 317 331 337 |
|||
353 359 373 379 397 401 409 421 443 449 |
|||
461 463 467 487 |
|||
found 54 Erdős primes < 500 |
|||
</pre> |
|||
=={{header|Ring}}== |
=={{header|Ring}}== |