Magnanimous numbers: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: added 1st/last digit parity test, optimized the MAGNA function.)
Line 430: Line 430:
The majority of the time consumed was in generated a list (sparse array) of suitable primes.
The majority of the time consumed was in generated a list (sparse array) of suitable primes.
<br>The '''genP''' subroutine could use a lot of optimization if wanted.
<br>The '''genP''' subroutine could use a lot of optimization if wanted.
<br>The '''magna''' function was quite simple to code and pretty fast.
<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.
<lang REXX>/*REXX pgm finds/displays magnanimous #s (#s with a inserted + sign to sum to a prime).*/
<lang REXX>/*REXX pgm finds/displays magnanimous #s (#s with a inserted + sign to sum to a prime).*/
parse arg bet.1 bet.2 bet.3 highP . /*obtain optional arguments from the CL*/
parse arg bet.1 bet.2 bet.3 highP . /*obtain optional arguments from the CL*/
Line 455: Line 456:
exit /*stick a fork in it, we're all done. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
magna: procedure expose @. !.; parse arg x /*obtain the number to be examined. */
magna: procedure expose @. !.; parse arg x 1 L 2 '' -1 R /*obtain #, 1st & last digit.*/
if x<10 then return 1 /*single digit numbers are magnanimous.*/
len= length(x); if len==1 then return 1 /*one digit #s are magnanimous*/
do s= 1 to length(x)-1 /*traipse thr number, inserting + sign.*/
if x>1001 then if L//2 == R//2 then return 0 /*Has parity? Not magnanimous*/
parse var x y +(s) z; sum= y + z /*parse 2 parts of #, sum 'em*/
do s= 1 for len-1 /*traipse thru #, inserting + */
if !.sum then iterate /*Sum is a prime? Then so far so good.*/
parse var x y +(s) z; sum= y + z /*parse 2 parts of #, sum 'em.*/
else return 0 /*Nope? Then not magnanimous*/
if !.sum then iterate /*Is sum prime? So far so good*/
end /*s*/
else return 0 /*Nope? Then not magnanimous.*/
end /*s*/
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; nP=6 /*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 /*assign primality to numbers. */
do lim=nP until lim>=highP; end /*only keep primes up to the sqrt(HI). */
do j=@.nP+4 by 2 to highP /*only find odd primes from here on. */
do j=@.nP+4 by 2 to highP /*only find odd primes from here on. */
if j// 3==0 then iterate /*is J divisible by #3 Then not prime*/
if j// 3==0 then iterate /*is J divisible by #3 Then not prime*/
Line 476: Line 477:
if j // @.k==0 then iterate j /*Is J divisible by P? Then not prime*/
if j // @.k==0 then iterate j /*Is J divisible by P? Then not prime*/
end /*k*/ /* [↓] a prime (J) has been found. */
end /*k*/ /* [↓] a prime (J) has been found. */
nP= nP+1; @.nP= j; !.j= 1 /*bump P cnt; assign P to @. and !. */
nP= nP+1; !.j=1; @.nP= j /*bump P cnt; assign P to @. and !. */
end /*j*/; return</lang>
end /*j*/; return</lang>
{{out|output|text=&nbsp; when using the default inputs:}}
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
<pre>