Humble numbers: Difference between revisions

Content added Content deleted
m (→‎{{header|Pascal}}: checked values with different float types)
Line 178: Line 178:


return 0;
return 0;
}</lang>
{{out}}
<pre>1 2 3 4 5 6 7 8 9 10 12 14 15 16 18 20 21 24 25 27 28 30 32 35 36 40 42 45 48 49 50 54 56 60 63 64 70 72 75 80 81 84 90 96 98 100 105 108 112 120

Of the first 32767 humble numbers:
9 have 1 digits
36 have 2 digits
95 have 3 digits
197 have 4 digits
356 have 5 digits
579 have 6 digits
882 have 7 digits
1272 have 8 digits
1767 have 9 digits</pre>

=={{header|D}}==
{{trans|C++}}
<lang d>import std.conv;
import std.stdio;

bool isHumble(int i) {
if (i <= 1) return true;
if (i % 2 == 0) return isHumble(i / 2);
if (i % 3 == 0) return isHumble(i / 3);
if (i % 5 == 0) return isHumble(i / 5);
if (i % 7 == 0) return isHumble(i / 7);
return false;
}

void main() {
auto limit = short.max;
int[int] humble;
auto count = 0;
auto num = 1;

while (count < limit) {
if (isHumble(num)) {
auto str = num.to!string;
auto len = str.length;
humble[len]++;
if (count < 50) write(num, ' ');
count++;
}
num++;
}
writeln('\n');

writeln("Of the first ", count, " humble numbers:");
num = 1;
while (num < humble.length - 1) {
if (num in humble) {
auto c = humble[num];
writefln("%5d have %2d digits", c, num);
num++;
} else {
break;
}
}
}</lang>
}</lang>
{{out}}
{{out}}