Magnanimous numbers: Difference between revisions

m
→‎{{header|REXX}}: optimized the GENP subroutine.
m (→‎{{header|REXX}}: optimized the GENP subroutine.)
Line 1,363:
=={{header|REXX}}==
The majority of the time consumed was in generating a list (sparse array) of suitable primes.
<br>The '''genP''' subroutine could use a lot of optimization if wanted.
<br>The '''magna''' function (magnanimous) was quite simple to code and pretty fast, it includes the 1<sup>st</sup> and last digit parity test.
<br>By far, the most CPU time was in the generation of primes.
Line 1,378 ⟶ 1,377:
if HI=='' then HI= LO /*Just a single number? Then use LO. */
if HI==0 then iterate /*Is HI a zero? Then skip this range.*/
#finds= 0; $= /*#: magnanimous # cnt; $: is a list*/
do k=0 until #finds==HI /* [↓] traipse through the number(s). */
if \magna(k) then iterate /*Not magnanimous? Then skip this num.*/
#= # + 1 finds= finds + 1 /*bump the magnanimous number count. */
if #finds>=LO then $= $ k /*In range► Then add number ──► $ list*/
end /*k*/
say
say center(' 'LO "──►" HI 'magnanimous numbers ', 126, "─")
say strip($)
end /*j*/
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
magna: procedure expose @. !.; parse arg x 1 L 2 '' -1 R /*obtain #, 1st & last digit.*/
Line 1,400 ⟶ 1,399:
return 1 /*Pass all the tests, it's magnanimous.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
genP: @.1=2; @.2=3; @.3=5; @.4=7; @.5=11; @.6= 13; nP=6 /*assign low primes; # primes.*/
!.= 0; !.2=1; !.3=1; !.5=1; !.7=1; !.11=1; !.13=1 /*assign primality " semaphores to numbers. " */
do j=@.nP+4 by 2 to highP #= 6; sq.#= @.# /*only* find2 odd primes from here on. /*# primes so far; P squared.*/
do j=@.#+4 by 2 to highP; parse var j '' -1 _; if _==5 then iterate /*Is÷ lastby digit a "5"? Then not prime*/
if j// 3==0 then iterate; if j// 7==0 then iterate /*is J divisible÷ by 3?; " ÷ by " " 7?*/
if j// 711==0 then iterate /* " " " " 7? /*" " 11? " " " 13?*/
if j//11==0 then iterate do k=6 while sq.k<=j /* " " /*divide by some " " 11? " " generated "odd primes. */
if j//13@.k==0 then iterate j /*Is "J "divisible by " " 13P? Then not " " " prime*/
end do /*k=7*/ while k*k<=j /*divide by[↓] some generateda oddprime primes (J) has been found. */
#= #+1; if@.#= j; // @ sq.k=#=0 j*j; then iterate !.j= 1 /*Isbump J#Ps; divisible byP──►@.assign P; P?^2; P Then not primeflag*/
end end /*kj*/; /* [↓] a prime (J) has been found. *return</lang>
nP= nP+1; !.j= 1; @.nP= j /*bump P cnt; assign P to @. and !. */
end /*j*/; return</lang>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>