Letd(n)bedefinedasthesumofproperdivisorsofn(numberslessthannwhichdivideevenlyinton). Ifd(a)=bandd(b)=a,wherea≠b,thenaandbareanamicablepairandeachofaandbarecalledamicablenumbers. Forexample,theproperdivisorsof220are1,2,4,5,10,11,20,22,44,5
If d(a) = b and d(b) = a, where a ≠ b, then a and b are an amicable pair and each of a and b are 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.
1. [代码][JavaScript]代码
<html> <head> </head> <body style="text-align=center;font-size:32px;"> <table align="center"> <tr><td><div id="problemNum" style='background-color:#999999;width:800;text-align:center;font-size:32px;'></div></td></tr> <tr><td><div id="problemContent" style='word-wrap:break-word;background-color:#bbbbbb;width:800;text-align:left;font-size:Npx;'></div></td></tr> <tr><td><div id="sum" style='word-wrap:break-word; color:#ffff22;font-size:48;background-color:#8855ff;width:800;text-align:center;'></div></td></tr> <tr><td><div id="copyleft" style='word-wrap:break-word; color:#ffff22;font-size:18;background-color:#666666;width:800;text-align:right;'></div></td></tr> <script language="javascript"> //---------------------------------// // Project Euler // // Author:thrombin // Date:2016-05-02 //---------------------------------// var p_order=21;//Problem Order var problem='Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n).<br/>\ If d(a) = b and d(b) = a, where a ≠ b, then a and b are an amicable pair and each of a and b are called amicable numbers.<br/>\ For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore d(220) = 284. <br/>\ The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220.<br/>\ <br/>\ Evaluate the sum of all the amicable numbers under 10000.'; //solve the problem //==============编程思路简介================ // 两步走:第一步求出10000以内所有数的真因子和 // 第二步逐个判断是否amicable,并求和 //========================================= var N=10000; var propDivs=new Array(N); var sum=0; for(var n=2;n<N;n++){ propDivs[n]=divisorSum(n); } function divisorSum(num){ var t_sum=1; for(var i=2;i<=Math.sqrt(num);i++){ if(num%i==0){ if(num==i*i){t_sum+=i;} else{t_sum+=i;t_sum+=num/i;} } } return t_sum; } for(n=2;n<N;n++){ if(propDivs[n]>1 && propDivs[n]<N){ //d(a)= b, data(b)=a,并且 a!=b if(n==propDivs[propDivs[n]] && n!=propDivs[n])sum+=(n); } } //update browser document.getElementById("problemNum").innerHTML="Project Euler-Problem "+p_order; document.getElementById("problemContent").innerHTML=problem; document.getElementById("sum").innerHTML="Answer:"+sum; document.getElementById("copyleft").innerHTML="CopyLeft@Thrombin 2016"; </script> </body> </html>