Magnanimous numbers: Difference between revisions

Content added Content deleted
(Add Factor)
(→‎{{header|REXX}}: added the REXX computer programming language for this task.)
Line 117: Line 117:
391st through 400th magnanimous numbers
391st through 400th magnanimous numbers
486685 488489 515116 533176 551558 559952 595592 595598 600881 602081</pre>
486685 488489 515116 533176 551558 559952 595592 595598 600881 602081</pre>

=={{header|REXX}}==
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 '''magna''' function was quite simple to code and pretty fast.
<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*/
if bet.1=='' | bet.1=="," then bet.1= 1..45 /* " " " " " " */
if bet.2=='' | bet.2=="," then bet.2= 241..250 /* " " " " " " */
if bet.3=='' | bet.3=="," then bet.3= 391..400 /* " " " " " " */
if highP=='' | highP=="," then highP= 1000000 /* " " " " " " */
call genP /*gen primes up to highP (1 million).*/

do j=1 for 3 /*process three magnanimous "ranges". */
parse var bet.j LO '..' HI /*obtain the first range (if any). */
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.*/
#= 0; $= /*#: magnanimous # cnt; $: is a list*/
do k=0 until #==HI /* [↑] traispe through the number(s). */
if \magna(k) then iterate /*Not magnanimous? Then skip this num.*/
#= # + 1 /*bump the magnanimous number count. */
if #>=LO then $= $ k /*In range► Then add number ──► $ list*/
end /*k*/
say
say center(' 'LO "──►" HI 'magnanimous numbers ', 126, "─")
say strip($)
end /*j*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
magna: procedure expose @. !.; parse arg x /*obtain the number to be examined. */
if x<10 then return 1 /*single digit numbers are magnanimous.*/
do s= 1 to length(x)-1 /*traipse thr number, inserting + sign.*/
parse var x y +(s) z; sum= y + z /*parse 2 parts of #, sum 'em*/
if !.sum then iterate /*Sum is a prime? Then so far so good.*/
else return 0 /*Nope? Then not magnanimous*/
end /*s*/
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 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. */
if j// 3==0 then iterate /*is J divisible by #3 Then not prime*/
parse var j '' -1 _;if _==5 then iterate /*Is last digit a "5"? " " " */
if j// 7==0 then iterate /*is J divisible by 7? " " " */
if j//11==0 then iterate /* " " " " 11? " " " */
if j//13==0 then iterate /*is " " " 13? " " " */
do k=7 while k*k<=j /*divide by some generated odd primes. */
if j // @.k==0 then iterate j /*Is J divisible by P? Then not prime*/
end /*k*/ /* [↓] a prime (J) has been found. */
nP= nP+1; @.nP= j; !.j= 1 /*bump P cnt; assign P to @. and !. */
end /*j*/; return</lang>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
──────────────────────────────────────────────── 1 ──► 45 magnanimous numbers ────────────────────────────────────────────────
0 1 2 3 4 5 6 7 8 9 11 12 14 16 20 21 23 25 29 30 32 34 38 41 43 47 49 50 52 56 58 61 65 67 70 74 76 83 85 89 92 94 98 101 110
and took 0.00 seconds.


────────────────────────────────────────────── 241 ──► 250 magnanimous numbers ───────────────────────────────────────────────
17992 19972 20209 20261 20861 22061 22201 22801 22885 24407
and took 0.31 seconds.


────────────────────────────────────────────── 391 ──► 400 magnanimous numbers ───────────────────────────────────────────────
486685 488489 515116 533176 551558 559952 595592 595598 600881 602081
<pre>