Tau number: Difference between revisions

Content added Content deleted
(PascalABC.NET)
(→‎{{header|REXX}}: overhauled)
Line 2,848: Line 2,848:


=={{header|REXX}}==
=={{header|REXX}}==
simplified,
<syntaxhighlight lang="rexx">/*REXX pgm displays N tau numbers, an integer divisible by the # of its divisors). */
made ooRexx compatible,
parse arg n cols . /*obtain optional argument from the CL.*/
corrected for tau of squares,
if n=='' | n=="," then n= 100 /*Not specified? Then use the default.*/
corrected for a range with fewer than cols numbers.
if cols=='' | cols=="," then cols= 10 /*Not specified? Then use the default.*/
w= max(8, length(n) ) /*W: used to align 1st output column. */
<syntaxhighlight lang="rexx">/* REXX program counts the number of divisors (tau,cr sigma_0) */
/* for a range of numbers */
@tau= ' the first ' commas(n) " tau numbers " /*the title of the tau numbers table. */
Parse Arg lo hi cols . /*obtain optional argument from the CL.*/
say ' index │'center(@tau, 1 + cols*(w+1) ) /*display the title of the output table*/
If lo=='' | lo==',' Then lo=1 /*Not specified? Then use the default.*/
say '───────┼'center("" , 1 + cols*(w+1), '─') /* " " header " " " " */
If hi=='' | hi==',' Then hi=lo+100-1 /*Not specified? Then use the default.*/
idx= 1; #= 0; $= /*idx: line; #: tau numbers; $: #s */
do j=1 until #==n /*search for N tau numbers */
If cols=='' Then cols=20 /* " " " " " " */
if j//tau(j) \==0 then iterate /*Is this a tau number? No, then skip.*/
w=2+(hi>45359) /* width of columns tau(4mns5360)=100 */
Say 'The number of divisors (tau) from' lo 'to' hi '(inclusive):'
#= # + 1 /*bump the count of tau numbers found. */
Say ''
$= $ right( commas(j), w) /*add a tau number to the output list. */
Say '-number' center(' tau (number of divisors) ',cols*(w+1)+1,'-')
if #//cols\==0 then iterate /*Not a multiple of cols? Don't show. */
line=''
say center(idx, 7)'│' substr($, 2) /*display partial list to the terminal.*/
c=0
idx= idx + cols; $= /*bump idx by number of cols; nullify $*/
index=lo
end /*j*/
Do j=lo To hi

c=c+1
if $\=='' then say center(idx, 7)"│" substr($, 2) /*possible display residual output.*/
line=line right(tau(j),w) /* add a tau number to the output line. */
say '───────┴'center("" , 1 + cols*(w+1), '─')
exit 0 /*stick a fork in it, we're all done. */
If c//cols=0 Then Do /* line has cols numbers */
Say center(index,7) line
/*──────────────────────────────────────────────────────────────────────────────────────*/
line=''
commas: parse arg ?; do jc=length(?)-3 to 1 by -3; ?=insert(',', ?, jc); end; return ?
cnt=0
/*──────────────────────────────────────────────────────────────────────────────────────*/
Index=index+cols
tau: procedure; parse arg x 1 y /*X and $ are both set from the arg.*/
End
if x<6 then return 2 + (x==4) - (x==1) /*some low #s should be handled special*/
End /*j*/
odd= x // 2 /*check if X is odd (remainder of 1).*/
If line\=='' Then
if odd then do; #= 2; end /*Odd? Assume divisor count of 2. */
else do; #= 4; y= x % 2; end /*Even? " " " " 4. */
Say center(index,7) line /*there any residuals left To display ? */
/* [↑] start with known number of divs*/
Exit 0 /*stick a fork in it, we're all done. */
/*---------------------------------------------------------------------------------*/
do j=3 for x%2-3 by 1+odd while j<y /*for odd number, skip even numbers. */
tau: Procedure
if x//j==0 then do /*if no remainder, then found a divisor*/
Parse Arg x
#= # + 2; y= x % j /*bump # of divisors; calculate limit.*/
if j>=y then do; #= # - 1; leave; end /*reached limit?*/
If x<6 Then /* some low numbers are handled special */
Return 2+(x==4)-(x==1)
end /* ___ */
tau=0
else if j*j>x then leave /*only divide up to x */
odd=x//2
end /*j*/ /* [↑] this form of DO loop is faster.*/
Do j=1 by 1 While j*j<x
return #</syntaxhighlight>
If odd & j//2=0 Then /* even j can't be a divisor of an odd x*/
Iterate
If x//j==0 Then /* if no remainder, then found a divisor*/
tau=tau+2 /* bump n of divisors */
End
If j*j=x Then /* x is a square */
tau=tau+1 /* its root is a divisor */
Return tau</syntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
{{out|output|text=&nbsp; when using the default input:}}
<pre>
<pre>
The number of divisors (tau) from 1 to 100 (inclusive):
index │ the first 100 tau numbers

───────┼───────────────────────────────────────────────────────────────────────────────────────────
-number ----------------- tau (number of divisors) ------------------
1 │ 1 2 8 9 12 18 24 36 40 56
11 60 72 80 84 88 96 104 108 128 132
1 1 2 2 3 2 4 2 4 3 4 2 6 2 4 4 5 2 6 2 6
21 136 152 156 180 184 204 225 228 232 240
21 4 4 2 8 3 4 4 6 2 8 2 6 4 4 4 9 2 4 4 8
31 248 252 276 288 296 328 344 348 360 372
41 2 8 2 6 6 4 2 10 3 6 4 6 2 8 4 8 4 4 2 12
41 376 384 396 424 441 444 448 450 468 472
61 2 4 6 7 4 8 2 6 4 8 2 12 2 4 6 6 4 8 2 10
51 480 488 492 504 516 536 560 564 568 584
81 5 4 2 12 4 4 4 8 2 12 4 6 4 4 4 12 2 6 6 9
61 │ 600 612 625 632 636 640 664 672 684 708
71 │ 712 720 732 776 792 804 808 824 828 852
81 │ 856 864 872 876 880 882 896 904 936 948
91 │ 972 996 1,016 1,040 1,044 1,048 1,056 1,068 1,089 1,096
───────┴───────────────────────────────────────────────────────────────────────────────────────────
</pre>
</pre>