Product of divisors: Difference between revisions

Content added Content deleted
(Add Fortran)
Line 396:
41 3111696 43 85184 91125
2116 47 254803968 343 125000</pre>
 
=={{header|D}}==
{{trans|C++}}
<lang d>import std.math;
import std.stdio;
 
// See https://en.wikipedia.org/wiki/Divisor_function
uint divisorCount(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;
}
 
uint divisorProduct(uint n) {
return cast(uint) pow(n, divisorCount(n) / 2.0);
}
 
void main() {
immutable limit = 50;
writeln("Product of divisors for the first ", limit, "positive integers:");
for (uint n = 1; n <= limit; n++) {
writef("%11d", divisorProduct(n));
if (n % 5 == 0) {
writeln;
}
}
}</lang>
{{out}}
<pre>Product of divisors for the first 50positive integers:
1 2 3 8 5
36 7 64 27 100
11 1728 13 196 225
1024 17 5832 19 8000
441 484 23 331776 124
676 729 21952 29 810000
31 32768 1089 1156 1225
10077696 37 1444 1521 2560000
41 3111696 43 85184 91125
2116 47 254803968 343 125000</pre>
 
=={{header|Factor}}==