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.*/ |
||
finds= 0; $= /*#: magnanimous # cnt; $: is a list*/ |
|||
do k=0 until |
do k=0 until finds==HI /* [↓] traipse through the number(s). */ |
||
if \magna(k) then iterate |
if \magna(k) then iterate /*Not magnanimous? Then skip this num.*/ |
||
finds= finds + 1 /*bump the magnanimous number count. */ |
|||
if |
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 |
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= |
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 /* |
!.= 0; !.2=1; !.3=1; !.5=1; !.7=1; !.11=1; !.13=1 /* " semaphores to " */ |
||
#= 6; sq.#= @.# ** 2 /*# primes so far; P squared.*/ |
|||
parse var j '' -1 _;if _==5 then iterate /* |
do j=@.#+4 by 2 to highP; parse var j '' -1 _; if _==5 then iterate /*÷ by 5?*/ |
||
if j// 3==0 then iterate |
if j// 3==0 then iterate; if j// 7==0 then iterate /*÷ by 3?; ÷ by 7?*/ |
||
if j// |
if j//11==0 then iterate /*" " 11? " " 13?*/ |
||
do k=6 while sq.k<=j /*divide by some generated odd primes. */ |
|||
if j// |
if j//@.k==0 then iterate j /*Is J divisible by P? Then not prime*/ |
||
end /*k*/ /* [↓] a prime (J) has been found. */ |
|||
#= #+1; @.#= j; sq.#= j*j; !.j= 1 /*bump #Ps; P──►@.assign P; P^2; P flag*/ |
|||
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= when using the default inputs:}} |
{{out|output|text= when using the default inputs:}} |
||
<pre> |
<pre> |