Magnanimous numbers: Difference between revisions

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