Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javaScript - Find the sum of all divisors of a given integer

i'm doing some coding exercises and i'm not being able to solve this one.

Find the sum of all divisors of a given integer. For n = 12, the input should be sumOfDivisors(n) = 28.

example: 1 + 2 + 3 + 4 + 6 + 12 = 28.

Constraints: 1 ≤ n ≤ 15.

how can i solve this exercise? i'm not being able to.

function(n){
    var arr = [],
        finalSum;

    if(n <= 1 || n => 16){
       return false ;   
   }
   for(var i = 0; i < n; i++){
       var tmp= n/2;
       arr.push(tmp)    
       // i need to keep on dividing n but i can't get the way of how to
   }
  return finalSum;
}
like image 959
Franco Manzur Avatar asked Dec 18 '25 06:12

Franco Manzur


1 Answers

This is another way to do it:

var divisors = n=>[...Array(n+1).keys()].slice(1)
   .reduce((s, a)=>s+(!(n % a) && a), 0);

console.log(divisors(12));

JSFiddle: https://jsfiddle.net/32n5jdnb/141/

Explaining:

  • n=> this is an arrow function, the equivalent to function(n) {. You don't need the () if there's only one parameter.
  • Array(n+1) creates an empty array of n+1 elements
  • .keys() gets the keys of the empty array (the indexes i.e. 0, 1, 2) so this is a way to create a numeric sequence
  • [...Array(n+1)].keys()] uses the spread (...) operator to transform the iterator in another array so creating an array with the numeric sequence
  • .slice(1) removes the first element thus creating a sequence starting with 1. Remember the n+1 ?
  • .reduce() is a method that iterates though each element and calculates a value in order to reduce the array to one value. It receives as parameter a callback function to calculate the value and the initial value of the calculation
  • (s, a)=> is the callback function for reduce. It's an arrow function equivalent to function(s, a) {
  • s+(!(n % a) && a) is the calculation of the value.
  • s+ s (for sum) or the last value calculated +
  • !(n % a) this returns true only for the elements that have a 0 as modular value.
  • (!(n % a) && a) is a js 'trick'. The case is that boolean expressions in javascript don't return true or false. They return a 'truthy' or 'falsy' value which is then converted to boolean. So the actual returned value is the right value for && (considering both have to be truthy) and the first thuthy value found for || (considering only one need to be truthy). So this basically means: if a is a modular value (i.e. != 0) return a to add to the sum, else return 0.
  • , 0 is the initial value for the reduce calculation.

Reduce documentation: https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce

Edit

Answering to Tristan Forward:

var divisorsList = [];
var divisors = (n)=>[...Array(n+1).keys()].slice(1)
       .reduce((s, a)=>{
          var divisor = !(n % a) && a;
          if (divisor) divisorsList.push(divisor);
          return s+divisor;
       }, 0);
    

console.log('Result:', divisors(12));
console.log('Divisors:', divisorsList);
like image 89
Nelson Teixeira Avatar answered Dec 20 '25 22:12

Nelson Teixeira



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!