Factors of an integer: Difference between revisions

Updated D versions
m (→‎{{header|Mercury}}: Corrected output to match the actual code's functionality.)
(Updated D versions)
Line 694:
 
=={{header|D}}==
<lang d>import std.stdio, std.math, std.algorithm;
 
T[] factor(T)(in T n) /*pure nothrow*/ {
if (n == 1) return [n];
 
T[] res = [1, n];
T limit = cast(T) sqrt(cast(real)n) + 1;
for (T i = 2; i < limit; i++) {
if (n % i == 0) {
res ~= i;
Tauto q = n / i;
if (q > i)
res ~= q;
}
}
 
return res.sort().release();
}
 
Line 726 ⟶ 727:
 
auto factors(I)(I n) {
return filter!((I i){ return=> n % i == 0; })(iota(1, n+1));
}
 
Anonymous user