Anonymous user
Extra primes: Difference between revisions
m
→{{header|REXX}}: optimized the generation of primes and "extra" primes, added some counts for increasing limit magnitudes.
Not a robot (talk | contribs) (Add Python) |
m (→{{header|REXX}}: optimized the generation of primes and "extra" primes, added some counts for increasing limit magnitudes.) |
||
Line 1,253:
=={{header|REXX}}==
Some optimization was done for the generation of primes, way more than was needed for this task's limit.
If the limit is negative, the list of primes found isn't shown, but the count of primes found is always shown.
<lang rexx>/*REXX pgm finds & shows all primes whose digits are prime and the digits sum to a prime*/
parse arg
if
xp= 1
$= 2
end /*k*/▼
$= /*a list that holds "extra" primes. */▼
▲ do j=1 for HI-1 /*search for numbers in this range. */
if verify(j, 2357) \== 0 then iterate /*J must be comprised of prime digits.*/
s= left(j, 1)
Line 1,270:
end /*k*/
if \!.s then iterate /*Is the sum not equal to prime? Skip.*/
if j<=hP then do
if \!.j then iterate /*is J a prime? No, then skip it. */
end /* _____ */
else do p=1 while @.p**2<=j /*perform division up to the √ J */
if j//@.p==0 then iterate j /*J divisible by a prime? Then ¬ prime*/
end /*p*/
if list then $= $ j /*maybe append extra prime ───► $ list.*/
end /*j*/
say
"and which are less than "
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
commas: parse arg ?; do jc=length(?)-3 to 1 by -3; ?=insert(',', ?, jc); end; return ?
/*──────────────────────────────────────────────────────────────────────────────────────*/
iSqrt: procedure; parse arg x; r= 0; q= 1; do while q<=x; q=q*4; end
do while q>1; q= q%4; _= x-r-q; r= r%2; if _>=0 then do; x= _; r= r+q; end
end /*while*/; return r
/*──────────────────────────────────────────────────────────────────────────────────────*/
genP: #= 9; @.1=2; @.2=3; @.3=5; @.4=7; @.5=11; @.6=13; @.7=17; @.8=19; @.9=23
!.=0; !.2=1; !.3=1; !.5=1; !.7=1; !.11=1; !.13=1; !.17=1; !.19=1; !.23=1
high= max(9 * digits(), iSqrt(hi) ) /*enough primes for sums & primality ÷ */
do j=29 by 2 while #<=high /*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?*/
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; @.#= j; !.j= 1 /*bump number of primes; assign prime#.*/
hP= @.#; return # /*hP: is the highest prime generated. */</lang>
{{out|output|text= when using the default input:}}
(Shown at three-quarter size.)
<pre style="font-size:75%">
36 primes found whose digits are prime and the digits sum to a prime and which are less than
2 3 5 7 23 223 227 337 353 373 557 577 733 757 773 2333 2357 2377 2557 2753 2777 3253 3257 3323 3527 3727 5233 5237 5273 5323 5527 7237 7253 7523 7723 7727
</pre>
{{out|output|text= when using the input: <tt> -100000 </tt>}}
<pre>
89 primes found whose digits are prime and the digits sum to a prime and which are less than 100,000.
</pre>
{{out|output|text= when using the input: <tt> -1000000 </tt>}}
<pre>
222 primes found whose digits are prime and the digits sum to a prime and which are less than 1,000,000.
</pre>
{{out|output|text= when using the input: <tt> -10000000 </tt>}}
<pre>
718 primes found whose digits are prime and the digits sum to a prime and which are less than 10,000,000.
</pre>
{{out|output|text= when using the input: <tt> -100000000 </tt>}}
<pre>
2,498 primes found whose digits are prime and the digits sum to a prime and which are less than 100,000,000.
</pre>
{{out|output|text= when using the input: <tt> -1000000000 </tt>}}
<pre>
9,058 primes found whose digits are prime and the digits sum to a prime and which are less than 1,000,000,000.
</pre>
|