Tau function: Difference between revisions

Content deleted Content added
MaiconSoft (talk | contribs)
Added Delphi example
Robbie (talk | contribs)
Line 222: Line 222:
5 4 2 12 4 4 4 8 2 12 4 6 4 4 4 12 2 6 6 9
5 4 2 12 4 4 4 8 2 12 4 6 4 4 4 12 2 6 6 9
</pre>
</pre>
=={{header|D}}==
{{trans|C++}}
<lang d>import std.stdio;

// See https://en.wikipedia.org/wiki/Divisor_function
uint divisor_count(uint n) {
uint total = 1;
// Deal with powers of 2 first
for (; (n & 1) == 0; n >>= 1) {
++total;
}
// Odd prime factors up to the square root
for (uint p = 3; p * p <= n; p += 2) {
uint 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;
}

void main() {
immutable limit = 100;
writeln("Count of divisors for the first ", limit, " positive integers:");
for (uint n = 1; n <= limit; ++n) {
writef("%3d", divisor_count(n));
if (n % 20 == 0) {
writeln;
}
}
}</lang>
{{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>

=={{header|Delphi}}==
=={{header|Delphi}}==
{{libheader| System.SysUtils}}
{{libheader| System.SysUtils}}