Fallon9111
Fallon9111

Reputation: 21

How to find numbers divisible by multiple numbers using javascript

I have taken a break from learning code. I came back and everything is gone from my head. I wanted to make a simple programme to find numbers divisible by any integers (eg divisble by 5,6,9,10 or 4,7,25)

I made this so far:

var multipleOf = function() {
    for (var i = 1; i < 1000; i*2) {
        if (i%3 === 0) {
            if (i%4 === 0) {
                if (i%5 === 0) {
                    return(i);
                }
            }
        }
    }
};

But there are a few problems. It doesn't seem to work as it is ( I think some python might be slipping into it) It isn't scalable, I have to change the code for a different amount of numbers (eg all prime numbers 1-100 would be a lot of coding as opposed to just typing in the numbers)

Can someone help me make code which could be run something like this:

console.log(multipleOf(2,5,8,12,15,17,20))

Upvotes: 2

Views: 6666

Answers (3)

ffflabs
ffflabs

Reputation: 17481

So far these solutions seem to point to building a function to tell if a number X is divisible by an array of integers [a,b,c,d].

I believe it would be more efficient to use something like the sieve of Eratostenes to avoid querying each possible number and instead build a list of numbers that do comply.

I'd start expanding the original input to every prime divisor of each value, then discarding the duplicates. From then on, we know that all multiples of the prime array [a,b,c] come in the form of

Math.pow(a,x) * Math.pow(b,y) * Math.pow(c,z)

And the only logic that's yet to be defined is in what order should I bump each of the powers to find the next number.

Upvotes: 0

Jashwant
Jashwant

Reputation: 29005

You can pass an array instead of multiple nos.

var multipleOf = function(nos) {
   for(var i=0; i< nos.length; i++) {
       // Now you can use,
      //nos[i]; to access each passed integer.
   }
};

var nos = [2,5,8,12,15,17,20];

multipleof(nos);

Or you can use the arguments

var multipleOf = function() {
   // Loop through arguments
   for(var i=0; i< arguments.length; i++) {
      // Now you can use,
      //arguments[i]; to access each passed integer.

   }
};

multipleOf(2,5,8,12,15,17,20);

i.e. arguments is a special type of array (It's not real actual though) and you can loop through it to accepts passed arguments.

Upvotes: -1

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324620

JavaScript has a neat thing for loosely defining arguments, aptly named arguments.

By my understanding, what you're actually looking for is the lowest common multiple of the supplied arguments, which is a fairly simple operation. Here's one way to do it, using helper functions to calculate the LCM of a pair of numbers, which in turn requires a function to calculate their GCD.

function multipleOf() {
    function gcf(a, b) { 
        return ( b == 0 ) ? a : gcf(b, a % b); 
    }
    function lcm(a, b) { 
        return a * b / gcf(a,b); 
    }
    function recurse(ar) {
        if (ar.length > 1) {
            // take the first two numbers, caculate their LCM, and put the result
            // back into the stack. Reduces array length by 1.
            ar.push( lcm( ar.shift() , ar.shift() ) );
            return recurse( ar );
        }
        else return ar[0];
    }
    // take arguments and build an array
    // arguments is an array-like object, but it doesn't have methods
    // such as `shift` or `push`, required for `recurse`
    // take this opportunity to ensure we have numbers.
    var ar = [], l = arguments.length, i;
    for( i=0; i<l; i++) ar[i] = parseInt(arguments[i],10);
    return recurse(ar);
}

Upvotes: 4

Related Questions