Count in factors: Difference between revisions

m
→‎{{header|REXX}}: added/changed whitespace and comments, optimized a part of primality testing.
No edit summary
m (→‎{{header|REXX}}: added/changed whitespace and comments, optimized a part of primality testing.)
Line 2,365:
It couldn't be determined if the   '''x'''  (for multiplication) was a strict requirement or
<br>whether there're blanks surrounding the &nbsp; '''x''' &nbsp; (blanks were assumed for this example for readability).
 
<br>There's commented code that shows how to not include blanks around the &nbsp; '''x''' &nbsp;
<br>[see comments about &nbsp; '''blanks=1''' &nbsp; &nbsp; (7<sup>th</sup> and 10<sup>th</sup> statements)].
<br>'''blanks=0''' &nbsp; will&nbsp; remove[see allcomments blanks around theabout &nbsp; '''xblanks=1''' &nbsp; &nbsp; (8<sup>th</sup> statement)].
 
<br><br>Also, as per the task's requirements, the prime factors of &nbsp; '''1''' &nbsp; (unity) will be listed as &nbsp; '''1''',
'''blanks=0''' &nbsp; will remove all blanks around the &nbsp; '''x'''.
 
 
<br><br>Also, as per the task's requirements, the prime factors of &nbsp; '''1''' &nbsp; (unity) will be listed as &nbsp; '''1''',
<br>even though, strictly speaking, it should be &nbsp; '''null'''. &nbsp; &nbsp; &nbsp; &nbsp; The same applies to &nbsp; '''0'''.
 
<br><br>Programming note: if the HIGH argument is negative, its positive value is used and no displaying of the
<br>prime factors are listed, but the number of primes found is always shown. &nbsp; The showing of the count of
<br>primes was included to help verify the factoring (of none primescomposites).
<lang rexx>/*REXX program lists the prime factors of a specified integer (or a range).*/
@.=left('',8); @.0='{unity} '; @.1='[prime] '; X='x' /*unitysome &tags primeand tagsliterals.*/
parse arg low high . /*get theoptional argument(s)arguments from the CLC.L. */
if low=='' then do;low=1;high=40;end /*No LOW & HIGH? Then use the default.*/
if high=='' then high=low; oHigh=high /*No HIGH? Then use the LOW. " " " " */
w=length(high); high=abs(high) /*get maxmaximum width for pretty telloutput. */
numeric digits max(9,w+1) /*maybe bump the precision of numbers. */
blanks=1 /*1=allow: allows spaces around the "x". */
primes#=0 /*the number of primes detected. found (so far). */
do n=low to high; f=factr(n) /*process a single number | or a range.*/
yp=factrwords(translate(f,,X))-(n==1); /*squishP: (oris not)the number theof prime blanksfactors. */
if p==1 then #=words(translate(y,,'x'))#+1 - (n==1) /*#bump ofthe primeprimes factors.counter (exclude N=1)*/
if #high\==1oHigh then primes=primes+1iterate /*bumponly show factors if HIGH primesis counter> (exclude0. 1)*/
say right(n,w) if high\='=oHigh' @.# thenspace(f,blanks) iterate /*only show factorsthe ifprime HIGHflag isand >0factors*/
end /*n*/ /*if BLANKS=0, then no spaces around X*/
say right(n,w) '=' @.# space(y,blanks) /*prime flag, factors.*/
end /*n*/ /*if BLANKS=0, no spaces around X*/
say
say right(primes#,w) ' primes found.' /*display the number of primes found. */
exit /*stick a fork in it, we're all done. */
/*────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────FACTR subroutine────────────────────*/
factr: procedure; expose X; parse arg x;q 1 x=abs(x)z,$; z=xif q<2 /*insure Xthen isreturn q /*0│1 positive.?*/
if x<2 then return x /*handle a[↓] couple specialprocess casesspecific low primes. */
list= do j=2 to 5; if j\==4 then call .add$; end /*nullifyfactorize theand listadd (empty──► string)list.*/
j=5 /*start [↓]where we processleft someoff low(with primesfive). */
do y=0 by 2; j=j+2+y//4 /*insure it's not divisible by three. */
do j=2 to 5; if j\==4 then call .buildF; end /*factorize, put──►list*/
j=5 parse var j '' -1 _ /*obtain the last decimal digit of J. /*start where we left off (five).*/
doif y_=0=5 bythen iterate 2; j=j+2+y//4 /*insurefast it'scheck not for divisible by 3five. */
if right( j,1)==5>z then iterateleave /*fastnumber reduced enough? ___ check for divisible by 5.*/
if j*j>zq then leave /*numberare reducedwe tohigher than the √ Q a small 'un? */
ifcall j*j>x.add$ then leave /*are we higher than the √X /*add ?a prime factor (J) to the $ list.*/
end /*y*/
call .buildF /*add a prime factor to list (J).*/
end /*y*/
 
if z==1 then z= /*if residual is = 1unity, then nullify it.*/
return strip(strip(list$ 'x'X z),,"x"X) /*elide a possible leading (extra) "x". */
/*────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────.BUILDF subroutine──────────────────*/
.buildFadd$: do while z//j==0; $=$ 'x' j; z=z%j; end; return /*keepadd dividing until it hurts.J─►$; integer ÷*/</lang>
'''output''' when using the defaultdefaults for inputsinput:
list=list 'x' j /*add number to the list (J). */
z=z%j /*do an integer divide. */
end /*while*/
return</lang>
'''output''' when using the default for inputs:
<pre style="height:40ex">
1 = {unity} 1
Line 2,459 ⟶ 2,458:
12 primes found.
</pre>
'''output''' when the following input was used: &nbsp; <tt> 1 &nbsp; -10000 </tt>
<pre>
1229 primes found.