Tau number: Difference between revisions

Content added Content deleted
(→‎{{header|REXX}}: overhauled)
(Undo revision 365760 by Walterpachl (talk))
Tag: Undo
Line 2,848: Line 2,848:


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

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

1 │ 1 2 8 9 12 18 24 36 40 56
-number ----------------- tau (number of divisors) ------------------
1 1 2 2 3 2 4 2 4 3 4 2 6 2 4 4 5 2 6 2 6
11 60 72 80 84 88 96 104 108 128 132
21 4 4 2 8 3 4 4 6 2 8 2 6 4 4 4 9 2 4 4 8
21 136 152 156 180 184 204 225 228 232 240
41 2 8 2 6 6 4 2 10 3 6 4 6 2 8 4 8 4 4 2 12
31 248 252 276 288 296 328 344 348 360 372
61 2 4 6 7 4 8 2 6 4 8 2 12 2 4 6 6 4 8 2 10
41 376 384 396 424 441 444 448 450 468 472
81 5 4 2 12 4 4 4 8 2 12 4 6 4 4 4 12 2 6 6 9
51 480 488 492 504 516 536 560 564 568 584
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>