Count in factors: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) (→{{header|Perl 6}}: Alternately, use a module) |
m (→{{header|REXX}}: added CLI support for specifying the "x" in the output (the multiplier characters), used a template for the output sedtions.) |
||
Line 3,256: | Line 3,256: | ||
<lang rexx>/*REXX program lists the prime factors of a specified integer (or a range of integers).*/ |
<lang rexx>/*REXX program lists the prime factors of a specified integer (or a range of integers).*/ |
||
@.=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 |
parse arg LO HI @ . /*get optional arguments from the C.L. */ |
||
if |
if LO=='' | LO=="," then do; LO=1; HI=40; end /*Not specified? Then use the default.*/ |
||
if |
if HI=='' | HI=="," then HI= LO /* " " " " " " */ |
||
if @=='' then @= 'x' /* " " " " " " */ |
|||
if length(@)\==1 then @= x2c(@) /*Not length 1? Then use hexadecimal. */ |
|||
tell= (HI>0) /*if HIGH is positive, then show #'s.*/ |
|||
HI= abs(HI) /*use the absolute value for HIGH. */ |
|||
w= length(HI) /*get maximum width for pretty output. */ |
|||
numeric digits max(9, w + 1) /*maybe bump the precision of numbers. */ |
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( |
do n=abs(LO) to HI; f= factr(n) /*process a single number or a range.*/ |
||
p= words( translate(f, , |
p= words( translate(f, ,@) ) - (n==1) /*P: is the number of prime factors. */ |
||
if p==1 then #= # + 1 /*bump the primes counter (exclude N=1)*/ |
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.*/ |
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) |
say right(#, w) ' primes found.' /*display the number of primes found. */ |
||
exit /*stick a fork in it, we're all done. */ |
exit /*stick a fork in it, we're all done. */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
factr: procedure |
factr: procedure expose @; parse arg z 1 n,$; if z<2 then return z /*is Z too small?*/ |
||
do while z//2==0; $= $ |
do while z//2==0; $= $||@||2; z= z%2; end /*maybe add factor of 2 */ |
||
do while z//3==0; $= $ |
do while z//3==0; $= $||@||3; z= z%3; end /* " " " " 3 */ |
||
do while z//5==0; $= $ |
do while z//5==0; $= $||@||5; z= z%5; end /* " " " " 5 */ |
||
do while z//7==0; $= $ |
do while z//7==0; $= $||@||7; z= z%7; end /* " " " " 7 */ |
||
do j=11 by 6 while j<=z /*insure that J isn't divisible by 3.*/ |
do j=11 by 6 while j<=z /*insure that J isn't divisible by 3.*/ |
||
parse var j '' -1 _ /*get the last decimal digit of J. */ |
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; $=$||@||j; z= z%j; end /*maybe reduce Z.*/ |
||
if _ ==3 then iterate /*Next # ÷ by 5? Skip. ___ */ |
if _ ==3 then iterate /*Next # ÷ by 5? Skip. ___ */ |
||
if j*j>n then leave /*are we higher than the √ N ? */ |
if j*j>n then leave /*are we higher than the √ N ? */ |
||
y= j + 2 /*obtain the next odd divisor. */ |
y= j + 2 /*obtain the next odd divisor. */ |
||
do while z//y==0; $=$ |
do while z//y==0; $=$||@||y; z= z%y; end /*maybe reduce Z.*/ |
||
end /*j*/ |
end /*j*/ |
||
if z==1 then return substr($, 1+length(@) ) /*Is residual=1? Don't add 1*/ |
|||
return substr($||@||z, 1+length(@) ) /*elide superfluous header. */</lang> |
|||
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,335: | Line 3,336: | ||
12 primes found. |
12 primes found. |
||
</pre> |
</pre> |
||
{{out|output|text= when the following input was used: <tt> 1 12 207820 </tt> |
|||
<pre> |
|||
1 = {unity} 1 |
|||
2 = [prime] 2 |
|||
3 = [prime] 3 |
|||
4 = 2 x 2 |
|||
5 = [prime] 5 |
|||
6 = 2 x 3 |
|||
7 = [prime] 7 |
|||
8 = 2 x 2 x 2 |
|||
9 = 3 x 3 |
|||
10 = 2 x 5 |
|||
11 = [prime] 11 |
|||
12 = 2 x 2 x 3 |
|||
5 primes found. |
|||
</pre> |
|||
{{out|output|text= when the following input was used: <tt> 1 -10000 </tt> |
|||
<pre> |
<pre> |
||
1229 primes found. |
1229 primes found. |
||
</pre> |
</pre> |
||
{{out|output|text= when the following input was used: <tt> 1 -100000 </tt> |
|||
<pre> |
<pre> |
||
9592 primes found. |
9592 primes found. |
||
Line 3,353: | Line 3,371: | ||
<lang rexx>/*REXX program lists the prime factors of a specified integer (or a range of integers).*/ |
<lang rexx>/*REXX program lists the prime factors of a specified integer (or a range of integers).*/ |
||
@.=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 |
parse arg LO HI @ . /*get optional arguments from the C.L. */ |
||
if |
if LO=='' | LO=="," then do; LO=1; HI=40; end /*Not specified? Then use the default.*/ |
||
if |
if HI=='' | HI=="," then HI= LO /* " " " " " " */ |
||
if @=='' then @= 'x' /* " " " " " " */ |
|||
if length(@)\==1 then @= x2c(@) /*Not length 1? Then use hexadecimal. */ |
|||
tell= (HI>0) /*if HIGH is positive, then show #'s.*/ |
|||
HI= abs(HI) /*use the absolute value for HIGH. */ |
|||
w= length(HI) /*get maximum width for pretty output. */ |
|||
numeric digits max(9, w + 1) /*maybe bump the precision of numbers. */ |
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( |
do n=abs(LO) to HI; f= factr(n) /*process a single number or a range.*/ |
||
p= words( translate(f, , |
p= words( translate(f, ,@) ) - (n==1) /*P: is the number of prime factors. */ |
||
if p==1 then #= # + 1 /*bump the primes counter (exclude N=1)*/ |
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.*/ |
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) |
say right(#, w) ' primes found.' /*display the number of primes found. */ |
||
exit /*stick a fork in it, we're all done. */ |
exit /*stick a fork in it, we're all done. */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
factr: procedure; parse arg z 1 n,$; |
factr: procedure expose @; parse arg z 1 n,$; if z<2 then return z /*is Z too small?*/ |
||
do while z// 2==0; |
do while z// 2==0; $= $||@||2 ; z= z%2 ; end /*maybe add factor of 2 */ |
||
do while z// 3==0; |
do while z// 3==0; $= $||@||3 ; z= z%3 ; end /* " " " " 3 */ |
||
do while z// 5==0; |
do while z// 5==0; $= $||@||5 ; z= z%5 ; end /* " " " " 5 */ |
||
do while z// 7==0; |
do while z// 7==0; $= $||@||7 ; z= z%7 ; end /* " " " " 7 */ |
||
do while z//11==0; |
do while z//11==0; $= $||@||11; z= z%11; end /* " " " " 11 */ |
||
do while z//13==0; |
do while z//13==0; $= $||@||13; z= z%13; end /* " " " " 13 */ |
||
do while z//17==0; |
do while z//17==0; $= $||@||17; z= z%17; end /* " " " " 17 */ |
||
do while z//19==0; |
do while z//19==0; $= $||@||19; z= z%19; end /* " " " " 19 */ |
||
do while z//23==0; |
do while z//23==0; $= $||@||23; z= z%23; end /* " " " " 23 */ |
||
do while z//29==0; |
do while z//29==0; $= $||@||29; z= z%29; end /* " " " " 29 */ |
||
do while z//31==0; |
do while z//31==0; $= $||@||31; z= z%31; end /* " " " " 31 */ |
||
do while z//37==0; |
do while z//37==0; $= $||@||37; z= z%37; end /* " " " " 37 */ |
||
if z>40 then do; t=z; q=1; r=0; do while q<=t; q= q * 4 |
if z>40 then do; t= z; q= 1; r= 0; 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 |
do while q>1; q=q%4; _=t-r-q; r=r%2; if _>=0 then do; t=_; r=r+q |
||
end |
end |
||
Line 3,390: | Line 3,411: | ||
do j=41 by 6 to r while j<=z /*insure J isn't divisible by 3*/ |
do j=41 by 6 to r while j<=z /*insure J isn't divisible by 3*/ |
||
parse var j '' -1 _ /*get last decimal digit of J.*/ |
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; $=$||@||j; z= z%j; end |
||
if _ ==3 then iterate /*Next number ÷ by 5 ? Skip.*/ |
if _ ==3 then iterate /*Next number ÷ by 5 ? Skip.*/ |
||
y= j + 2 /*use the next (odd) divisor. */ |
y= j + 2 /*use the next (odd) divisor. */ |
||
do while z//y==0; $=$ |
do while z//y==0; $=$||@||y; z= z%y; end |
||
end /*j*/ /* [↑] reduce Z by Y ? */ |
end /*j*/ /* [↑] reduce Z by Y ? */ |
||
end /*if z>40*/ |
end /*if z>40*/ |
||
if z==1 then return |
if z==1 then return substr($, 1+length(@) ) /*Is residual=1? Don't add 1*/ |
||
return |
return substr($||@||z, 1+length(@) ) /*elide superfluous header. */</lang> |
||
{{out|output|text= |
{{out|output|text= when using the default inputs:}} |
||
<pre> |
|||
1 = {unity} 1 |
|||
2 = [prime] 2 |
|||
3 = [prime] 3 |
|||
4 = 2∙2 |
|||
5 = [prime] 5 |
|||
6 = 2∙3 |
|||
7 = [prime] 7 |
|||
8 = 2∙2∙2 |
|||
9 = 3∙3 |
|||
10 = 2∙5 |
|||
11 = [prime] 11 |
|||
12 = 2∙2∙3 |
|||
13 = [prime] 13 |
|||
14 = 2∙7 |
|||
15 = 3∙5 |
|||
16 = 2∙2∙2∙2 |
|||
17 = [prime] 17 |
|||
18 = 2∙3∙3 |
|||
19 = [prime] 19 |
|||
20 = 2∙2∙5 |
|||
21 = 3∙7 |
|||
22 = 2∙11 |
|||
23 = [prime] 23 |
|||
24 = 2∙2∙2∙3 |
|||
25 = 5∙5 |
|||
26 = 2∙13 |
|||
27 = 3∙3∙3 |
|||
28 = 2∙2∙7 |
|||
29 = [prime] 29 |
|||
30 = 2∙3∙5 |
|||
31 = [prime] 31 |
|||
32 = 2∙2∙2∙2∙2 |
|||
33 = 3∙11 |
|||
34 = 2∙17 |
|||
35 = 5∙7 |
|||
36 = 2∙2∙3∙3 |
|||
37 = [prime] 37 |
|||
38 = 2∙19 |
|||
39 = 3∙13 |
|||
40 = 2∙2∙2∙5 |
|||
12 primes found. |
|||
</pre> |
|||
=={{header|Ring}}== |
=={{header|Ring}}== |