Count in factors: Difference between revisions
Content added Content deleted
m (→using integer SQRT: used better highlighting for the REXX section header.) |
m (→{{header|REXX}}: optimized the function, simplified some code, added/changed whitespace and comments.) |
||
Line 3,227: | Line 3,227: | ||
@.=left('', 8); @.0="{unity} "; @.1='[prime] ' /*some tags and handy-dandy literals.*/ |
@.=left('', 8); @.0="{unity} "; @.1='[prime] ' /*some tags and handy-dandy literals.*/ |
||
parse arg low high . /*get optional arguments from the C.L. */ |
parse arg low high . /*get optional arguments from the C.L. */ |
||
if low=='' then do; low=1; high=40; end |
if low=='' then do; low= 1; high= 40; end /*No LOW & HIGH? Then use the default.*/ |
||
if high=='' then high=low |
if high=='' then high= low /*No HIGH? " " " " */ |
||
tell= (high>0) /*if HIGH is positive, then show #'s.*/ |
|||
high= abs(high) /*use the absolute value for HIGH. */ |
|||
w= length(high) /*get maximum width for pretty output. */ |
|||
numeric digits max(9, w + 1) /*maybe bump the precision of numbers. */ |
|||
#= 0 /*the number of primes found (so far). */ |
|||
do n=abs(low) to high; f= factr(n) /*process a single number or a range.*/ |
|||
p= words( translate(f, ,'x') ) - (n==1) /*P: is the number of prime factors. */ |
|||
if p==1 then #= # + 1 /*bump the primes counter (exclude N=1)*/ |
|||
⚫ | |||
if tell then say right(n, w) '=' @.p f /*display if a prime, plus its factors.*/ |
|||
⚫ | |||
say |
say |
||
say right(#, w) ' primes found.' /*display the number of primes found. */ |
say right(#, w) ' primes found.' /*display the number of primes found. */ |
||
Line 3,242: | Line 3,244: | ||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
factr: procedure; parse arg z 1 n,$; if z<2 then return z /*if Z too small, return Z*/ |
factr: procedure; parse arg z 1 n,$; if z<2 then return z /*if Z too small, return Z*/ |
||
do while z// |
do while z//2==0; $= $'x2'; z= z % 2; end /*maybe add factor of 2 */ |
||
do while z// |
do while z//3==0; $= $'x3'; z= z % 3; end /* " " " " 3 */ |
||
do while z// |
do while z//5==0; $= $'x5'; z= z % 5; end /* " " " " 5 */ |
||
do while z// |
do while z//7==0; $= $'x7'; z= z % 7; end /* " " " " 7 */ |
||
do j=11 by 6 while j<=z |
do j=11 by 6 while j<=z /*insure that J isn't divisible by 3.*/ |
||
parse var j '' -1 _ |
parse var j '' -1 _ /*get the last decimal digit of J. */ |
||
if _\==5 then do while z//j==0; |
if _\==5 then do while z//j==0; $=$'x'j; z= z%j; end /*maybe reduce Z.*/ |
||
if _ ==3 then iterate |
if _ ==3 then iterate /*Next # ÷ by 5? Skip. ___ */ |
||
if j*j>n then leave |
if j*j>n then leave /*are we higher than the √ N ? */ |
||
⚫ | |||
y=j+2 |
|||
do while z//y==0; |
do while z//y==0; $=$'x'y; z= z%y; end /*maybe reduce Z.*/ |
||
end /*j*/ |
end /*j*/ |
||
if z==1 then return strip($ , , 'x') /*if residual is unity, then nullify it*/ |
|||
return strip($'x'z, , 'x') /*elide a possible leading (extra) "x".*/</lang> |
|||
{{out|output|text= when using the default inputs:}} |
{{out|output|text= when using the default inputs:}} |
||
<pre> |
<pre> |
||
Line 3,322: | Line 3,324: | ||
@.=left('', 8); @.0="{unity} "; @.1='[prime] ' /*some tags and handy-dandy literals.*/ |
@.=left('', 8); @.0="{unity} "; @.1='[prime] ' /*some tags and handy-dandy literals.*/ |
||
parse arg low high . /*get optional arguments from the C.L. */ |
parse arg low high . /*get optional arguments from the C.L. */ |
||
if low=='' then do; |
if low=='' then do; low= 1; high= 40; end /*No LOW & HIGH? Then use the default.*/ |
||
if high=='' then high= low |
if high=='' then high= low /*No HIGH? " " " " */ |
||
tell= (high>0) /*if HIGH is positive, then show #'s.*/ |
|||
high= abs(high) /*use the absolute value for HIGH. */ |
|||
w= length(high) /*get maximum width for pretty output. */ |
|||
numeric digits max(9, w + 1) /*maybe bump the precision of numbers. */ |
|||
#= 0 /*the number of primes found (so far). */ |
#= 0 /*the number of primes found (so far). */ |
||
do n=abs(low) to high; f= factr(n) |
do n=abs(low) to high; f= factr(n) /*process a single number or a range.*/ |
||
p= words( translate(f, , |
p= words( translate(f, ,'x') ) - (n==1) /*P: is the number of prime factors. */ |
||
if p==1 then #= # + 1 |
if p==1 then #= # + 1 /*bump the primes counter (exclude N=1)*/ |
||
if tell then say right(n, w) |
if tell then say right(n, w) '=' @.p f /*display if a prime, plus its factors.*/ |
||
end /*n*/ |
end /*n*/ |
||
say |
say |
||
say right(#, w) ' primes found.' /*display the number of primes found. */ |
say right(#, w) ' primes found.' /*display the number of primes found. */ |
||
Line 3,337: | Line 3,341: | ||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
factr: procedure; parse arg z 1 n,$; if z<2 then return z /*if Z too small, return Z*/ |
factr: procedure; parse arg z 1 n,$; if z<2 then return z /*if Z too small, return Z*/ |
||
do while z// 2==0; $= $ |
do while z// 2==0; $= $'x2' ; z= z%2 ; end /*maybe add factor of 2 */ |
||
do while z// 3==0; $= $ |
do while z// 3==0; $= $'x3' ; z= z%3 ; end /* " " " " 3 */ |
||
do while z// 5==0; $= $ |
do while z// 5==0; $= $'x5' ; z= z%5 ; end /* " " " " 5 */ |
||
do while z// 7==0; $= $ |
do while z// 7==0; $= $'x7' ; z= z%7 ; end /* " " " " 7 */ |
||
do while z//11==0; $= $ |
do while z//11==0; $= $'x11'; z= z%11; end /* " " " " 11 */ |
||
do while z//13==0; $= $ |
do while z//13==0; $= $'x13'; z= z%13; end /* " " " " 13 */ |
||
do while z//17==0; $= $ |
do while z//17==0; $= $'x17'; z= z%17; end /* " " " " 17 */ |
||
do while z//19==0; $= $ |
do while z//19==0; $= $'x19'; z= z%19; end /* " " " " 19 */ |
||
do while z//23==0; $= $ |
do while z//23==0; $= $'x23'; z= z%23; end /* " " " " 23 */ |
||
do while z//29==0; $= $ |
do while z//29==0; $= $'x29'; z= z%29; end /* " " " " 29 */ |
||
do while z//31==0; $= $ |
do while z//31==0; $= $'x31'; z= z%31; end /* " " " " 31 */ |
||
do while z//37==0; $= $ |
do while z//37==0; $= $'x37'; z= z%37; end /* " " " " 37 */ |
||
r= 0 /*R: will be integer SQRT of Z.*/ |
|||
if z>40 then do; t= z; q= 1; do while q<=t; q= q * 4; end /*while*/ |
if z>40 then do; t= z; q= 1; do while q<=t; q= q * 4; end /*while*/ |
||
do while q>1; q= q%4; _= t-r-q; r= r%2; if _>=0 then do; t=_; r=r+q |
|||
end |
|||
end /*while*/ /* [↑] find integer SQRT(z). */ |
|||
⚫ | |||
do j=41 by 6 to r while j<=z |
do j=41 by 6 to r while j<=z /*insure J isn't divisible by 3*/ |
||
parse var j '' -1 _ |
parse var j '' -1 _ /*get last decimal digit of J.*/ |
||
if _\==5 then do while z//j==0; |
if _\==5 then do while z//j==0; $=$'x'j; z= z%j; end |
||
if _ ==3 then iterate /*Next number ÷ by 5 ? Skip.*/ |
|||
y= j + 2 /*use the next (odd) divisor. */ |
|||
do while z//y==0; $=$'x'y; z= z%y; end |
|||
end /*j*/ /* [↑] reduce Z by Y ? */ |
|||
end /*if z>40*/ |
|||
end /*j*/ |
|||
end /*if z>40*/ |
|||
if z==1 then |
if z==1 then return strip($ , , 'x') /*if residual is unity, then nullify it*/ |
||
return strip( |
return strip($'x'z, , 'x') /*elide a possible leading (extra) "x".*/</lang> |
||
{{out|output|text= is identical to the 1<sup>st</sup> REXX version.}}<br><br> |
{{out|output|text= is identical to the 1<sup>st</sup> REXX version.}}<br><br> |
||