RosettaCodeData/Task/Casting-out-nines/D/casting-out-nines.d
2015-02-20 09:02:09 -05:00

27 lines
697 B
D

import std.stdio, std.algorithm, std.range;
uint[] castOut(in uint base=10, in uint start=1, in uint end=999999) {
auto ran = iota(base - 1)
.filter!(x => x % (base - 1) == (x * x) % (base - 1));
auto x = start / (base - 1);
immutable y = start % (base - 1);
typeof(return) result;
while (true) {
foreach (immutable n; ran) {
immutable k = (base - 1) * x + n;
if (k < start)
continue;
if (k > end)
return result;
result ~= k;
}
x++;
}
}
void main() {
castOut(16, 1, 255).writeln;
castOut(10, 1, 99).writeln;
castOut(17, 1, 288).writeln;
}