Tau number: Difference between revisions

Content deleted Content added
Walterpachl (talk | contribs)
Undo revision 365760 by Walterpachl (talk)
Tag: Undo
Chkas (talk | contribs)
 
(2 intermediate revisions by 2 users not shown)
Line 860:
856 864 872 876 880 882 896 904 936 948
972 996 1016 1040 1044 1048 1056 1068 1089 1096
</pre>
 
=={{header|C sharp|C#}}==
<syntaxhighlight lang="csharp">
internal class Program
{
private static void Main(string[] args)
{
long limit = 100;
Console.WriteLine($"The first {limit} tau numbers are:");
long count = 0;
 
for (long n = 1; count < limit; ++n)
{
if (IsTauNumber(n))
{
Console.Write($"{n, 6} ");
++count;
 
if (count % 10 == 0)
{
Console.WriteLine();
}
}
}
}
 
private static bool IsTauNumber(long n)
{
return n % DivisorCount(n) == 0;
}
 
private static long DivisorCount(long n)
{
long total = 1;
// Deal with powers of 2 first
for (; (n & 1) == 0; n >>= 1)
{
++total;
}
 
// Odd prime factors up to the square root
for (long p = 3; p * p <= n; p += 2)
{
long count = 1;
 
for (; n % p == 0; n /= p)
{
++count;
}
 
total *= count;
}
 
// If n > 1 then it's prime
if (n > 1)
{
total *= 2;
}
 
return total;
}
}
</syntaxhighlight>
{{out}}
<pre>
The first 100 tau numbers are:
1 2 8 9 12 18 24 36 40 56
60 72 80 84 88 96 104 108 128 132
136 152 156 180 184 204 225 228 232 240
248 252 276 288 296 328 344 348 360 372
376 384 396 424 441 444 448 450 468 472
480 488 492 504 516 536 560 564 568 584
600 612 625 632 636 640 664 672 684 708
712 720 732 776 792 804 808 824 828 852
856 864 872 876 880 882 896 904 936 948
972 996 1016 1040 1044 1048 1056 1068 1089 1096
</pre>
 
Line 1,240 ⟶ 1,317:
 
=={{header|EasyLang}}==
<syntaxhighlight lang=text>
func cntdiv n .
i = 1
Line 2,848 ⟶ 2,925:
 
=={{header|REXX}}==
Simplified, use the tau function of the respective task, ooRexx compatible
<syntaxhighlight lang="rexx">/*REXX pgm displays N tau numbers, an integer divisible by the # of its divisors). */
<syntaxhighlight lang="rexx">/*REXX pgm displays N tau numbers (integers divisible by the # of its divisors). */
parse arg n cols . /*obtain optional argument from the CL.*/
ifParse Arg n cols . n=='' | n=="," then n= 100 /*Notobtain specified?optional argument Then usefrom the defaultCL. */
ifIf cols n=='' | cols=="," then colsn==',' 10 Then n= 100 /*Not specified? Then use the default. */
If cols=='' | cols==',' Then cols= 10 /*Not specified? Then use the default. */
w= max(8, length(n) ) /*W: used to align 1st output column. */
@tauw=6 ' the first ' commas(n) " tau numbers " /*theW: titleused ofTo thealign tau1st numbersoutput tablecolumn. */
say ttau=' indexthe first 'center(@tau, 1 + cols*commas(w+1n) ' tau )numbers' /*display the title of the output table. */
saySay '───────┼ index ¦'center("" ttau, 1 + cols*(w+1), '─') /* ") /* display the title " header " " " " */
Say '-------+'center('' ,cols*(w+1),'-')
idx= 1; #= 0; $= /*idx: line; #: tau numbers; $: #s */
idx=1
do j=1 until #==n /*search for N tau numbers */
nn=0 if j//tau(j) \==0 then iterate /*Is thisnumber aof tau number?numbers No, then skip.*/
dd=''
#= # + 1 /*bump the count of tau numbers found. */
Do j=1 Until nn==n $= $ right( commas(j), w) /*add asearch for N tau numbernumbers to the output list. */
If j//tau(j)==0 Then Do if #//cols\==0 then iterate /* If this /*Notis a multipletau number of cols? Don't show. */
nn=nn+1 say center(idx, 7)'│' substr($, 2) /*display partialbump listthe tocount theof terminaltau numbers found. */
idxdd=dd idxright(commas(j),w) + cols; $= /*bump idxadd bya tau number ofTo the cols;output nullifylist. $*/
If nn//cols==0 Then Do end /*j a line is full */
Say center(idx,7)'¦' substr(dd,2) /* display partial list To the terminal.*/
 
idx= idx+cols /* bump idx by number of cols */
if $\=='' then say center(idx, 7)"│" substr($, 2) /*possible display residual output.*/
dd=''
say '───────┴'center("" , 1 + cols*(w+1), '─')
End
exit 0 /*stick a fork in it, we're all done. */
End
/*──────────────────────────────────────────────────────────────────────────────────────*/
End
commas: parse arg ?; do jc=length(?)-3 to 1 by -3; ?=insert(',', ?, jc); end; return ?
If dd\=='' Then Say center(idx,7)'¦' substr(dd,2) /*possible display rest */
/*──────────────────────────────────────────────────────────────────────────────────────*/
Say '--------'center('' ,cols*(w+1),'-')
tau: procedure; parse arg x 1 y /*X and $ are both set from the arg.*/
Exit 0 if x<6 then return 2 + (x==4) - (x==1) /*somestick a fork in it,we're lowall #sdone. should be handled special*/
/*--------------------------------------------------------------------------------*/
odd= x // 2 /*check if X is odd (remainder of 1).*/
commas: Parse Arg ?; Do jc=length(?)-3 To 1 by -3; ?=insert(',',?,jc); End; Return ?
if odd then do; #= 2; end /*Odd? Assume divisor count of 2. */
/*--------------------------------------------------------------------------------*/
else do; #= 4; y= x % 2; end /*Even? " " " " 4. */
tau: Procedure
/* [↑] start with known number of divs*/
Parse Arg x
do j=3 for x%2-3 by 1+odd while j<y /*for odd number, skip even numbers. */
If x<6 Then if x//j==0 then do /*if nosome low remainder,numbers thenare foundhandled aspecial divisor*/
Return 2+(x==4)-(x==1)
#= # + 2; y= x % j /*bump # of divisors; calculate limit.*/
tau=0
if j>=y then do; #= # - 1; leave; end /*reached limit?*/
odd=x//2
end /* ___ */
Do j=1 by 1 While j*j<x
else if j*j>x then leave /*only divide up to √ x */
If odd & end j/*j*/2=0 Then /* even j can't be a /* [↑] this formdivisor of DO loopan isodd faster.x*/
Iterate
return #</syntaxhighlight>
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
-------+----------------------------------------------------------------------
───────┼───────────────────────────────────────────────────────────────────────────────────────────
1 ¦ 1 2 8 9 12 18 24 36 40 56
11 ¦ 60 72 80 84 88 96 104 108 128 132
21 ¦ 136 152 156 180 184 204 225 228 232 240
31 ¦ 248 252 276 288 296 328 344 348 360 372
41 ¦ 376 384 396 424 441 444 448 450 468 472
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>
 
=={{header|Ring}}==