Tau number: Difference between revisions

2,686 bytes added ,  10 days ago
Undo revision 365760 by Walterpachl (talk)
(→‎{{header|REXX}}: overhauled)
(Undo revision 365760 by Walterpachl (talk))
Tag: Undo
Line 2,848:
 
=={{header|REXX}}==
<syntaxhighlight lang="rexx">/*REXX pgm displays N tau numbers, an integer divisible by the # of its divisors). */
simplified,
Parseparse Argarg lo hin cols . /*obtain optional argument from the CL.*/
made ooRexx compatible,
Ifif lo n=='' | lo n=='",'" Then lothen n=1 100 /*Not specified? Then use the default.*/
corrected for tau of squares,
Ifif hicols=='' | hicols=='",'" Then hithen cols=lo+100-1 10 /*Not specified? Then use the default.*/
corrected for a range with fewer than cols numbers.
<syntaxhighlight langw="rexx">/* REXXmax(8, programlength(n) counts) the number of divisors (tau,cr sigma_0) /*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.*/
If j*jidx=x Then1; #= 0; /* x is a square $= /*idx: line; #: tau numbers; $: #s */
If hi=='' | hi==',' Then hi=lo+100-1 /*Not specified? Then use the default.*/
If cols=='' do j=1 Thenuntil cols#=20=n /* " " /*search for " N " tau "numbers " */
w=2+(hi>45359) if j//tau(j) \==0 then iterate /*Is this a tau number? /* width of columns tau(4mns5360)=100No, then skip.*/
#= # + 1 /*bump the count of tau numbers found. */
Say 'The number of divisors (tau) from' lo 'to' hi '(inclusive):'
line $=line $ right(tau commas(j), w) /* add a tau number to the output linelist. */
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 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. */
exit 0 If c//cols=0 Then Do /* line has cols numbers /*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
tauodd=tau+2 x // 2 /* bump n of divisors /*check if X is odd (remainder of 1).*/
End /*j*/
If if odd & j//2=0then Thendo; #= 2; end /*Odd? even j can't be aAssume divisor count of an odd2. x*/
If line\=='' Then
Say center(index,7) line else do; #= 4; y= x % 2; end /*thereEven? any residuals left To display ?" " " " 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 if x//j==0 Thenthen 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 if j>=y then do; #= # /*- some1; low numbers areleave; handled special end /*reached limit?*/
/* for a range of numbers end /* ___ */
Return 2+(x==4)-(x==1)
tau=tau+1 else if j*j>x then leave /*only itsdivide rootup isto a divisor x */
tau=0
end /*j*/ /* [↑] this form of DO loop is faster.*/
odd=x//2
Return tau 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:}}
<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) ------------------
11 1 1 2 60 2 3 2 472 2 4 3 80 4 2 6 284 4 4 5 88 2 6 2 696 104 108 128 132
21 4 4 136 2 8 3 152 4 4 6 156 2 8 2 180 6 4 4 184 4 9 2 204 4 4 8 225 228 232 240
4131 2 8 248 2 6 6 252 4 2 10 3276 6 4 6288 2 8 4296 8 4 4328 2 12 344 348 360 372
6141 2 4 376 6 7 4 384 8 2 6 396 4 8 2 12424 2 4 6441 6 4 8444 2 10 448 450 468 472
8151 5 4 480 2 12 4 4488 4 8 2492 12 4 6 504 4 4 4 12516 2 6 6536 9 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>
 
2,300

edits