Anonymous user
Count in factors: Difference between revisions
m
→{{header|REXX}}: optimized the function, simplified some code, added/changed whitespace and comments.
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:
@.=left('', 8); @.0="{unity} "; @.1='[prime] ' /*some tags and handy-dandy literals.*/
parse arg low high . /*get optional arguments from the C.L. */
if low=='' then do; low= 1; high= 40; end
if high=='' then high= low
numeric digits max(9, w
#= 0
if p==1 then #= # + 1 /*bump the primes counter (exclude N=1)*/
end /*n*/▼
if tell then say right(n, w) '=' @.p f /*display if a prime, plus its factors.*/
▲ end /*n*/
say
say right(#, w) ' primes found.' /*display the number of primes found. */
Line 3,242 ⟶ 3,244:
/*──────────────────────────────────────────────────────────────────────────────────────*/
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//
do while z//
do while z//
do j=11 by 6 while j<=z
parse var j '' -1 _
if _\==5 then do while z//j==0;
if _ ==3 then iterate /*Next # ÷ by
if j*j>n then leave
do while z//y==0;
end /*j*/
{{out|output|text= when using the default inputs:}}
<pre>
Line 3,322 ⟶ 3,324:
@.=left('', 8); @.0="{unity} "; @.1='[prime] ' /*some tags and handy-dandy literals.*/
parse arg low high . /*get optional arguments from the C.L. */
if low=='' then do;
if high=='' then high= low
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)
p= words( translate(f, ,
if p==1 then #= # + 1
if tell then say right(n, w)
end /*n*/
say
say right(#, w) ' primes found.' /*display the number of primes found. */
Line 3,337 ⟶ 3,341:
/*──────────────────────────────────────────────────────────────────────────────────────*/
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// 3==0; $= $
do while z// 5==0; $= $
do while z// 7==0; $= $
do while z//11==0; $= $
do while z//13==0; $= $
do while z//17==0; $= $
do while z//19==0; $= $
do while z//23==0; $= $
do while z//29==0; $= $
do while z//31==0; $= $
do while z//37==0; $= $
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*/
end /*while*/ /* [↑] find integer SQRT(z).
▲ end /*while*/ /* [↑] find integer SQRT(z). */
do j=41 by 6 to r while j<=z
parse var j '' -1 _
if _\==5 then do while z//j==0;
if _ ==3 then iterate
end /*j*/
if z==1 then
return strip(
{{out|output|text= is identical to the 1<sup>st</sup> REXX version.}}<br><br>
|