Jump to content

Tau function: Difference between revisions

(PascalABC.NET)
(→‎{{header|REXX}}: overhauled)
Line 2,487:
 
=={{header|REXX}}==
simplified,
<syntaxhighlight lang="rexx">/*REXX program counts the number of divisors (tau, or sigma_0) up to and including N.*/
made ooRexx compatible,
parse arg LO HI cols . /*obtain optional argument from the CL.*/
corrected for tau of squares,
if LO=='' | LO=="," then LO= 1 /*Not specified? Then use the default.*/
corrected for a range with fewer than cols numbers.
if HI=='' | HI=="," then HI= LO + 100 - 1 /*Not specified? Then use the default.*/
if<syntaxhighlight cols=='' | cols=lang=",rexx" then cols= 20 >/* "REXX program counts the number of "divisors (tau,cr sigma_0) " " " " */
w=/* for a range of numbers 2 + (HI>45359) /*W: used to align the output columns.*/
sayParse 'TheArg numberlo ofhi divisorscols . (tau) for integers up to ' n " (inclusive):"; /*obtain sayoptional argument from the CL.*/
ifIf LOlo=='' | LOlo=="',"' Then then LOlo= 1 /*Not specified? Then use the default.*/
say '─index─' center(" tau (number of divisors) ", cols * (w+1) + 1, '─')
ifIf HIhi=='' | HIhi=="',"' Then then HIhi= LO lo+ 100 -1 1 /*Not specified? Then use the default.*/
$=; c= 0 /*$: the output list, shown ROW/line.*/
If cols=='' Then cols=20 do j=LO to HI; /* " c= c +" 1 /*list # proper divisors (tau) 1" " " " ──► N */
w=2+(hi>45359) $= $ right( tau(j), w) /*add awidth tauof numbercolumns to the outputtau(4mns5360)=100 list. */
Say 'The number of divisors (tau) from' lo 'to' hi '(inclusive):'
if c//cols \== 0 then iterate /*Not a multiple of ROW? Don't display.*/
Say ''
idx= j - cols + 1 /*calculate index value (for this row).*/
saySay '─index─-number' center("' tau (number of divisors) "', cols * (w+1) + 1, '-')
say center(idx, 7) $; $= /*display partial list to the terminal.*/
line=''
end /*j*/
c=0
 
index=lo
if $\=='' then say center(idx+cols, 7) $ /*there any residuals left to display ?*/
Do j=lo To hi
exit 0 /*stick a fork in it, we're all done. */
c=c+1
/*──────────────────────────────────────────────────────────────────────────────────────*/
tau: procedure; parse arg x 1 y line=line right(tau(j),w) /*X add anda tau $number to arethe bothoutput setline. from the arg.*/
If c//cols=0 Then Do if x<6 then return 2 + (x==4) - (x==1) /*some lowline has cols numbers #s should be handled special*/
Say center(index,7) line
odd= x // 2 /*check if X is odd (remainder of 1).*/
line=''
if odd then #= 2 /*Odd? Assume divisor count of 2. */
cnt=0
else do; #= 4; y= x % 2; end /*Even? " " " " 4. */
Index=index+cols
/* [↑] start with known number of divs*/
End
do j=3 for x%2-3 by 1+odd while j<y /*for odd number, skip even numbers. */
End /*j*/
if x//j==0 then do /*if no remainder, then found a divisor*/
If line\=='' Then
#= # + 2; y= x % j /*bump # of divisors; calculate limit.*/
Say center(index,7) line /*there any residuals left ifTo j>=ydisplay ? then do; #= # - 1; leave; end /*reached limit?*/
Exit 0 end /*stick a fork in it, we're all ___ done. */
/*---------------------------------------------------------------------------------*/
else if j*j>x then leave /*only divide up to √ x */
tau: Procedure
end /*j*/; return # /* [↑] this form of DO loop is faster.*/</syntaxhighlight>
Parse Arg x
If x<6 Then /* [↑] some startlow withnumbers knownare numberhandled ofspecial divs*/
Return 2+(x==4)-(x==1)
tau=0
odd=x//2
Do j=1 by 1 While j*j<x
ifIf odd & then #j//2=0 2 Then /*Odd? even j can't Assumebe a divisor count of an 2. odd x*/
Iterate
ifIf x//j==0 thenThen do /* if no remainder, then found a divisor*/
$ tau=;tau+2 /* bump n of divisors c= 0 /*$: the output list, shown ROW/line.*/
End
exit 0 If j*j=x Then /* x is a square /*stick a fork in it, we're all done. */
oddtau=tau+1 x // 2 /* its root is a divisor /*check if X is odd (remainder of 1).*/
Return tau</syntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
The number of divisors (tau) from for integers up1 to 100 (inclusive):
 
-number ----------------- tau (number of divisors) ------------------
─index─ ──────────────────────────── 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
21 4 4 2 8 3 4 4 6 2 8 2 6 4 4 4 9 2 4 4 8
41 2 8 2 6 6 4 2 10 3 6 4 6 2 8 4 8 4 4 2 12
61 2 4 6 7 4 8 2 6 4 8 2 12 2 4 6 6 4 8 2 10
81 5 4 2 12 4 4 4 8 2 12 4 6 4 4 4 12 2 6 6 9
</pre>
 
2,299

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.