Amicable numbers -- Javascript 实现

问题描述:

Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n).

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.

实现:

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

})();

Amicable numbers -- Javascript 实现,布布扣,bubuko.com

时间: 2024-10-09 23:29:03

Amicable numbers -- Javascript 实现的相关文章

Project Euler:Problem 21 Amicable numbers

Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n). 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

projecteuler----&gt;problem=21----Amicable numbers

Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n). 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 d

JavaScript basics_Foundational Part 1_Numbers

Javascript Numbers 知识描述:JavaScript 只有一种数字类型,即数字(Number).数字可以带小数点,也可以不带,也就是整数和小数. 数字可以带小数点,也可以不带: Example var x = 23; // 使用小数点来写 var x = 23.00; // 不使用小数点来写 极大或极小的数字可以通过科学(指数)计数法来书写: Example var x = 23e4; // 2230000 var y = 23e-4; // 0.0023 JavaScript

Python练习题 048:Project Euler 021:10000以内所有亲和数之和

本题来自 Project Euler 第21题:https://projecteuler.net/problem=21 ''' Project Euler: Problem 21: Amicable numbers Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n). If d(a) = b and d(b) = a, where a ≠ b

Project-Euler problem 1-50

最近闲的做了下Project Euler 上的题目,前面50题都比较简单,简单总结下.代码一般是Python和C/C++的 用Python 做这些题目简直是酸爽啊 一下代码可能不一定是我的,因为不知道论坛里面的回复不是永久的,所以我的代码有的丢了,可能找个和我的意思相近的代码.题目翻译是从 欧拉计划 | Project Euler 中文翻译站上面Copy 的表告我. Problem 1  Multiples of 3 and 5 10以下的自然数中,属于3和5的倍数的有3,5,6和9,它们之和是

JavaScript- The Good Parts CHAPTER 2

I know it well:I read it in the grammar long ago.—William Shakespeare, The Tragedy(悲剧:灾难:惨案) of Titus Andronicus This chapter introduces the grammar of the good parts of JavaScript, presenting a quick overview of how the language is structured. We wi

欧拉计划21-23题

21.Amicable numbers Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n). 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

MongoDB:The Definitive Guide CHAPTER 2 Getting Started

MongoDB is very powerful, but it is still easy to get started with. In this chapter we’ll introduce some of the basic concepts of MongoDB: • A document is the basic unit of data for MongoDB, roughly equivalent to a row in a relational database manage

《程序设计技术》课程辅助学习资料

本文档提供课程相关的辅助学习资料. 阅读程序是提高程序设计水平的最为有效的方法,<程序设计技术>课程至少应该阅读后面提供链接博文中的基础部分.能够阅读完基础部分的博文,则可以给课程学习奠定一个坚实的基础. 自己编写程序也是学习编程必不可少的一个环节.自己编写是否正确可以通过OJ系统来验证.选做OJ的程序设计题方便于评价自己所写的程序是否正确.想要提高编写程序的能力并且达到更高的水平,从各个OJ中选做一些编程题是十分必要的. 文中提供了CCF-CSP认证考试历年 试题的第1题的题解.这些题解中都