javaScript-查找给定整数的所有除数的总和 [英] javaScript - Find the sum of all divisors of a given integer
本文介绍了javaScript-查找给定整数的所有除数的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在做一些编码练习,但我无法解决这个问题.
i'm doing some coding exercises and i'm not being able to solve this one.
查找给定整数的所有除数之和.对于n = 12,输入应为sumOfDivisors(n)= 28.
Find the sum of all divisors of a given integer. For n = 12, the input should be sumOfDivisors(n) = 28.
例如:1 + 2 + 3 + 4 + 6 + 12 = 28.
example: 1 + 2 + 3 + 4 + 6 + 12 = 28.
约束: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;
}
推荐答案
这是另一种方法:
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/
JSFiddle: https://jsfiddle.net/32n5jdnb/141/
说明:
-
n =>
这是一个箭头函数,等效于function(n){.如果只有一个参数,则不需要(). -
Array(n + 1)
创建一个由n + 1个元素组成的空数组 -
.keys()
获取空数组的键(索引,即0、1、2),这是创建数字序列的一种方法 -
[... Array(n + 1)].keys()]
使用spread(...)运算符将迭代器转换为另一个数组,从而使用数字序列创建一个数组 -
.slice(1)
删除第一个元素,从而创建一个以1开头的序列.还记得n + 1吗? -
.reduce()
是一种遍历每个元素并计算值以将数组缩减为一个值的方法.它接收一个回调函数作为参数,以计算计算的值和初始值 -
(s,a)=>
是reduce的回调函数.这是一个箭头函数,等效于函数{s,a} { -
s +(!(n%a)&& a)
是该值的计算. -
s +
s(用于求和)或最后计算的值+ -
!(n%a)
仅对具有0作为模数值的元素返回true. -
(!(n%a)&& a)
是js技巧".情况是javascript中的布尔表达式不返回true或false.它们返回真实"或虚假"值,然后将其转换为布尔值.因此,实际的返回值是&&(考虑到两者都必须是真实的)和||的第一个thothy值.(只考虑一个事实).因此,这基本上意味着:如果a
是一个模块化值(即!= 0),则返回a
加上总和,否则返回0. -
,0
是归约计算的初始值.
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: ifa
is a modular value (i.e. != 0) returna
to add to the sum, else return 0., 0
is the initial value for the reduce calculation.
减少文档: https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce
修改
向特里斯坦前进的答案:
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);
这篇关于javaScript-查找给定整数的所有除数的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文