`
mybwu_com
  • 浏览: 176031 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

Amicable numbers -- Javascript 实现

 
阅读更多

问题描述:


Let d(n) be defined as the sum of proper divisors ofn(numbers less thannwhich divide evenly inton).
If d(a) =band d(b) =a, wherea≠b, thenaandbare an amicable pair and each ofaandbare called amicable numbers.

For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220.

Evaluate the sum of all the amicable numbers under 10000.

实现:

(function(){

var factor = function (n){
var arr = new Array();
for(var i = 1;i < n; i++)
{
if(n%i == 0 && arr.indexOf(i) == -1)
arr.push(i);
}
return arr;
}

var sumArr = function(arr){
var sum = 0 ;

for(var i = 0 ; i < arr.length; i++)
sum += arr[i];

return sum ;
}


for(var i = 2;i< 10000; i++)
{
var r1 = sumArr(factor(i));
var r2 = sumArr(factor(r1));

if(i == r2 && i != r1)
console.log("num1 : " + i + ", num2 : " + r1);
}

})();



分享到:
评论

相关推荐

    is-amicable:知道数字是否友好的简单函数

    友善的 知道数字是否友好的简单函数安装npm i - S is - amicable用法var isAmicable = require ( 'is-amicable' ) ;console . log ( isAmicable ( 220 ) ) ; // trueconsole . log ( isAmicable ( 284 ) ) ; // ...

    8000词必记词根-8000词必记词根

    amicable 友善的amiable 和蔼可亲的 anim 心灵,精神,生命 animal 动物animate 有生命的 ann ,enn 年 annual 一年的centennial 一世纪的 astro 星 astronomy 天文学astronaut 宇宙航行员 audi 听 audience 听众...

    雅思口语素材

    Being amicable Granted he is as busy as scores of celebrities, he never poses up with a poker face. In stead, he always takes the initiative to greet and communicate with others. Being considerate ...

    友好数:计算两个整数、上限和下限之间的友好对的函数。-matlab开发

    将 'amicable.m' 文件放入您的 MATLAB 路径或工作目录并调用以下语法。 函数语法: 输出=友好(N2,N1) 其中,N2 为上限,N1 为下限,均为正整数。 如果只给出一个数字,则认为是上限, 默认的下限是 220(因为...

    Beej's Guide to Unix IPC

    How do you get these guys to talk to one another, share data structures, and be generally amicable? This document discusses several methods of Interprocess Communication (IPC) that can accomplish this...

    用C#编写的汇编集成环境

    uses EDIT、MASM、LINK、DEBUG four steps of DOS commands pattern mainly , whose step is miscellaneous and trivial , the interface is not amicable , brings teaching and using about certain difficulty ...

    简历个人品质常用英文词汇

    amicable 友好的 analytical 善于分析的 apprehensive 有理解力的 aspiring 有志气的,有抱负的 audacious 大胆的,有冒险精神的 capable 有能力的,有才能的 careful 办理仔细的 candid 正直的

Global site tag (gtag.js) - Google Analytics