问题描写叙述:

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 实现的更多相关文章

  1. (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 ...

  2. Eloquent JavaScript #01# values

    When action grows unprofitable, gather information; when information grows unprofitable, sleep.      ...

  3. Javascript——概述 && 继承 && 复用 && 私有成员 && 构造函数

    原文链接:A re-introduction to JavaScript (JS tutorial) Why a re-introduction? Because JavaScript is noto ...

  4. JavaScript Basics_Fundamentals Part 1_Numbers

    Javascript Numbers 知识描述:JavaScript 只有一种数字类型,即数字(Number).数字可以带小数点,也可以不带,也就是整数和小数. 数字可以带小数点,也可以不带: Exa ...

  5. JavaScript 转换数字为整数的方法

    本文将会列举并说明JavaScript 把一个number(或者numerical的对象)转换成一个整数相关方法. 使用parseInt parseInt的语法如下:parseInt(string, ...

  6. JavaScript- The Good Parts CHAPTER 2

    I know it well:I read it in the grammar long ago.—William Shakespeare, The Tragedy(悲剧:灾难:惨案) of Titu ...

  7. 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 ...

  8. PE刷题记

    PE 中文翻译 最喜欢做这种很有意思的数学题了虽然数学很垃圾 但是这个网站的提交方式好鬼畜啊qwq 1.Multiples of 3 and 5 直接枚举 2.Even Fibonacci numbe ...

  9. Problem 21

    Problem 21 https://projecteuler.net/problem=21 Let d(n) be defined as the sum of proper divisors of  ...

随机推荐

  1. html 自定义标签使用实现方法

    通过指定html命名空间的名字来定义自定义标签:默认的一些标签p div等都在html默认的命名空间下.而自定义的标签可以放在自定义的命名空间下,可通过xmlns:命名空间名 来指定,而自定义标签需要 ...

  2. Mysql 删除重复记录,只保留最小的一条

    delete from `jb_postcontent` where id not in(select min(id) from (select * from `jb_postcontent`) as ...

  3. C#中访问私有成员

    首先访问一个类的私有成员不是什么好做法.大家都知道私有成员在外部是不能被访问的.一个类中会存在很多私有成员:如私有字段.私有属性.私有方法.对于私有成员造访,可以套用下面这种非常好的方式去解决. pr ...

  4. Java中断机制(interrupt)

    中断线程 在 run() 方法中,如果语句执行到了最会一句,或是遇到 return 方法,或是方法中出现了没有被捕获的异常,run() 方法将会执行结束.在java中,Thread中的interrup ...

  5. [转]Oracle High Water Level高水位分析

    PLSQL_性能优化系列14_Oracle High Water Level高水位分析 http://www.cnblogs.com/eastsea/p/4005814.html 一.摘要 PLSQL ...

  6. Struts2初探

    我记得美妙的瞬间:在我的面前出现了你,有如昙花一现的幻影 今天写一篇Struts2框架的,在很久很久以前,Struts2可谓是称霸江湖,纵然现在有后起之秀,但Struts2依然可以成为老牌的主流框架, ...

  7. linux-之常用命令

    Linux常用命令,长时间不用或者想用时具体的使用方法模糊了,可以进行查看,避免还要去其他地方进行查找麻烦,所以找了一些命令进行记录.   1.帮助命令 help 和 man 帮助查看命令的具体使用方 ...

  8. Tomcat多域名访问

    对于域名解析相信很多小伙伴都了解过,就是我们在万网购买一个域名,比如hpugs.com,然后呢?我们希望域名与我们的服务器绑定,然后通过域名直接访问我们的项目,这就是本篇要和大家一起探讨的问题.下面开 ...

  9. Chapter 6: The Memory Hierarchy

    Disk Geometry: 磁盘的结构如图,每个面为surface,surface上的同心圆为track,track包含sector,不同的surface的同半径track构成cylinder.越外 ...

  10. springboot(十六):使用Jenkins部署Spring Boot

    jenkins是devops神器,本篇文章介绍如何安装和使用jenkins部署Spring Boot项目 jenkins搭建 部署分为三个步骤: 第一步,jenkins安装 第二步,插件安装和配置 第 ...