RosettaCodeData/Task/Hamming-numbers/D/hamming-numbers-1.d

25 lines
616 B
D

import std.stdio, std.bigint, std.algorithm, std.range, core.memory;
auto hamming(in uint n) pure nothrow /*@safe*/ {
immutable BigInt two = 2, three = 3, five = 5;
auto h = new BigInt[n];
h[0] = 1;
BigInt x2 = 2, x3 = 3, x5 = 5;
size_t i, j, k;
foreach (ref el; h.dropOne) {
el = min(x2, x3, x5);
if (el == x2) x2 = two * h[++i];
if (el == x3) x3 = three * h[++j];
if (el == x5) x5 = five * h[++k];
}
return h.back;
}
void main() {
GC.disable;
iota(1, 21).map!hamming.writeln;
1_691.hamming.writeln;
1_000_000.hamming.writeln;
}