Tau function: Difference between revisions

Added C# example
(→‎{{header|REXX}}: overhauled)
(Added C# example)
 
Line 894:
2 4 6 7 4 8 2 6 4 8 2 12 2 4 6 6 4 8 2 10
5 4 2 12 4 4 4 8 2 12 4 6 4 4 4 12 2 6 6 9
</pre>
 
=={{header|C sharp|C#}}==
<syntaxhighlight lang="csharp">
internal class Program
{
private static void Main(string[] args)
{
long limit = 100;
Console.WriteLine($"Count of divisors for the first {limit} positive integers:");
long count = 0;
 
for (long n = 1; count < limit; ++n)
{
Console.Write($"{DivisorCount(n),3} ");
++count;
 
if (count % 20 == 0)
{
Console.WriteLine();
}
}
}
 
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>
Count of divisors for the first 100 positive integers:
1 2 2 3 2 4 2 4 3 4 2 6 2 4 4 5 2 6 2 6
4 4 2 8 3 4 4 6 2 8 2 6 4 4 4 9 2 4 4 8
2 8 2 6 6 4 2 10 3 6 4 6 2 8 4 8 4 4 2 12
2 4 6 7 4 8 2 6 4 8 2 12 2 4 6 6 4 8 2 10
5 4 2 12 4 4 4 8 2 12 4 6 4 4 4 12 2 6 6 9
</pre>
 
9

edits