Extra primes: Difference between revisions

m
→‎{{header|REXX}}: optimized the generation of primes and "extra" primes, added some counts for increasing limit magnitudes.
(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 HIhi . /*obtain optional argument from the CL.*/
if HIhi=='' | HIhi=="," then HIhi= 10000 /*Not specified? Then use the default.*/
ylist= 2hi>=0; 3 5 7 11 13 17 19 23 29 31 37 41 43 47hi= abs(hi) 53 59 61 67 71 73 79/*set 83a 89switch; 97 101use 103the 107absolute 109value*/
@.=call genP 0; !.= @. /*defineinvoke defaultssubroutine forto primesgenerate & indicesprimes.*/
xp= 1 do k=1 for words(y); p= word(y, k) /*obtain a prime /*number fromof theextra list.primes found (so far)*/
$= 2 @.k= p; !.p= 1 /*define a primelist bythat it'sholds index"extra" & valueprimes. */
do j=13 by 2 for HI(hi-1 )%2 /*search for numbers in this range. */
end /*k*/
#= 0
$= /*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 do p=1 while @.p**2<=j /*performJ divisionmay upbe tosmall theenough sqrtto ofsee J.if prime*/
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*/
#xp= #xp + 1; $= $ j /*bump #the count; appendof Jprimes to thefound $so list.far*/
if list then $= $ j /*maybe append extra prime ───► $ list.*/
end /*j*/
say #commas(xp) ' primes found whose digits are prime and the digits sum to a prime' ,
"and which are less than " HIcommas(hi)word(. ":", list + 1)
say strip($) if list then say $ /*maybe display the output list to───► the termterminal. */</lang>
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?*/
$= /*a list[↓] that holdsdivide "extra"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; @.#= j; !.j= 1 /*bump number of primes; assign prime#.*/
end /*kj*/
hP= @.#; return # /*hP: is the highest prime generated. */</lang>
{{out|output|text=&nbsp; 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 1000010,000:
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=&nbsp; when using the input: &nbsp; &nbsp; <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=&nbsp; when using the input: &nbsp; &nbsp; <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=&nbsp; when using the input: &nbsp; &nbsp; <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=&nbsp; when using the input: &nbsp; &nbsp; <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=&nbsp; when using the input: &nbsp; &nbsp; <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>