n-smooth   numbers are positive integers which have no prime factors  >  n.

The   n   (when using it in the expression)   n-smooth   is always prime,

there are   no   9-smooth numbers.

1   (unity)   is always included in n-smooth numbers.




2-smooth   numbers are non-negative powers of two.

5-smooth   numbers are also called   Hamming numbers.

7-smooth   numbers are also called    humble   numbers.



A way to express   11-smooth   numbers is:

  11-smooth  =  2i × 3j × 5k × 7m × 11p

           where     i, j, k, m, p ≥ 0  



Task

   calculate and show the first   25   n-smooth numbers   for   n=2   ───►   n=29
   calculate and show   three numbers starting with   3,000   n-smooth numbers   for   n=3   ───►   n=29
   calculate and show twenty numbers starting with  30,000   n-smooth numbers   for   n=503   ───►   n=521   (optional)


All ranges   (for   n)   are to be inclusive, and only prime numbers are to be used.

The (optional) n-smooth numbers for the third range are:   503,   509,   and   521.

Show all n-smooth numbers for any particular   n   in a horizontal list.

Show all output here on this page.



Related tasks

   Hamming numbers
   humble numbers


References

   Wikipedia entry:   Hamming numbers     (this link is re-directed to   Regular number).
   Wikipedia entry:   Smooth number
   OEIS entry:   A000079    2-smooth numbers or non-negative powers of two
   OEIS entry:   A003586    3-smooth numbers
   OEIS entry:   A051037    5-smooth numbers or Hamming numbers
   OEIS entry:   A002473    7-smooth numbers or humble numbers
   OEIS entry:   A051038   11-smooth numbers
   OEIS entry:   A080197   13-smooth numbers
   OEIS entry:   A080681   17-smooth numbers
   OEIS entry:   A080682   19-smooth numbers
   OEIS entry:   A080683   23-smooth numbers


import std.algorithm;
import std.bigint;
import std.exception;
import std.range;
import std.stdio;
 
BigInt[] primes;
int[] smallPrimes;
 
bool isPrime(BigInt value) {
    if (value < 2) return false;
 
    if (value % 2 == 0) return value == 2;
    if (value % 3 == 0) return value == 3;
 
    if (value % 5 == 0) return value == 5;
    if (value % 7 == 0) return value == 7;
 
    if (value % 11 == 0) return value == 11;
    if (value % 13 == 0) return value == 13;
 
    if (value % 17 == 0) return value == 17;
    if (value % 19 == 0) return value == 19;
 
    if (value % 23 == 0) return value == 23;
 
    BigInt t = 29;
    while (t * t < value) {
        if (value % t == 0) return false;
        value += 2;
 
        if (value % t == 0) return false;
        value += 4;
    }
 
    return true;
}
 
// cache all primes up to 521
void init() {
    primes ~= BigInt(2);
    smallPrimes ~= 2;
 
    BigInt i = 3;
    while (i <= 521) {
        if (isPrime(i)) {
            primes ~= i;
            if (i <= 29) {
                smallPrimes ~= i.toInt;
            }
        }
        i += 2;
    }
}
 
BigInt[] nSmooth(int n, int size)
in {
    enforce(n >= 2 && n <= 521, "n must be between 2 and 521");
    enforce(size > 1, "size must be at least 1");
}
do {
    BigInt bn = n;
    bool ok = false;
    foreach (prime; primes) {
        if (bn == prime) {
            ok = true;
            break;
        }
    }
    enforce(ok, "n must be a prime number");
 
    BigInt[] ns;
    ns.length = size;
    ns[] = BigInt(0);
    ns[0] = 1;
 
    BigInt[] next;
    foreach(prime; primes) {
        if (prime > bn) {
            break;
        }
        next ~= prime;
    }
 
    int[] indicies;
    indicies.length = next.length;
    indicies[] = 0;
    foreach (m; 1 .. size) {
        ns[m] = next.reduce!min;
        foreach (i,v; indicies) {
            if (ns[m] == next[i]) {
                indicies[i]++;
                next[i] = primes[i] * ns[indicies[i]];
            }
        }
    }
 
    return ns;
}
 
void main() {
    init();
 
    foreach (i; smallPrimes) {
        writeln("The first ", i, "-smooth numbers are:");
        writeln(nSmooth(i, 25));
        writeln;
    }
    foreach (i; smallPrimes.drop(1)) {
        writeln("The 3,000th to 3,202 ", i, "-smooth numbers are:");
        writeln(nSmooth(i, 3_002).drop(2_999));
        writeln;
    }
    foreach (i; [503, 509, 521]) {
        writeln("The 30,000th to 30,019 ", i, "-smooth numbers are:");
        writeln(nSmooth(i, 30_019).drop(29_999));
        writeln;
    }
}