Using JavaScript to implement amicable numbers

Source: Internet
Author: User

Problem Description:

Let d (n) is defined as the sum of proper divisors of n (numbers less than n which divide into N).
If d (a) = B and d (b) = A, where a B, then A and B are a amicable pair and each of a and B are called amicable numbers.

For example, the proper divisors of the are 1, 2, 4, 5, A,,, and 110; therefore d (220) = 284. The proper divisors of 284 are 1, 2, 4, and 142; So d (284) = 220.

Evaluate the sum of all the amicable numbers under 10000.

Realize:

(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);  
}"  
      
) ();

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/script/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.