programming-examples/js/Math/Show the Hamming numbers.js
2019-11-15 12:59:38 +01:00

22 lines
712 B
JavaScript

function Hamming(n) {
var succession = [1];
var length = succession.length;
var candidate = 2;
while (length < n) {
if (isHammingNumber(candidate)) {
succession[length] = candidate;
length++;
}
candidate++;
}
return succession;
}
function isHammingNumber(num) {
while (num % 5 === 0) num /= 5;
while (num % 3 === 0) num /= 3;
while (num % 2 === 0) num /= 2;
return num == 1;
}
console.log(Hamming(20));