JSFiddle - React, Tailwind, and code Playground

by Kai

JavaScript

// Return all the factors of n
function factors (n) {
    var f = [],
        i = 1;
    
    for (; i <= n; i++) {
        if (n % i === 0) f.push(i);
    }
    return f;
}

// Return all the multiples of n up to x
function multiples (n, x) {
    var m = [],
        i = 1,
        last_product = n;
    
    while (last_product < x) {
        m.push( last_product );
        last_product = (n * ++i); 
    }

    return m;
}


// Return all the prime numbers up to n
function primes (n) {
    var p = [],
        i = 1;
    
    for (; i <= n; i++) {
        if (i % 2 !== 0 && i % 3 !== 0) p.push(i);
    }
    
    return p;
}

// Return all the twin primes up to n
function twin_primes (n) {
    var t = [],
        p = primes(n),
        i = 0, max = p.length;
    
    for (; i < max; i++) {
        if ( (p[i+1] - p[i]) === 2 ) t.push([p[i],p[i+1]]);
    }
    return t;
}

// Return all the composite numbers up to n
function composites (n) {
    var c = [],
        i = 1;
    
    for (; i <= n; i++) {
        if (i % 2 === 0 || i % 3 === 0) c.push(i);
    }
    
    return c;
}

//console.log( factors(112) );
//console.log( multiples(3, 25) );
//console.log( primes(100) );
//console.log( twin_primes(100) );
console.log( composites(100) );