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 实现的更多相关文章
- (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 ...
- Eloquent JavaScript #01# values
When action grows unprofitable, gather information; when information grows unprofitable, sleep. ...
- Javascript——概述 && 继承 && 复用 && 私有成员 && 构造函数
原文链接:A re-introduction to JavaScript (JS tutorial) Why a re-introduction? Because JavaScript is noto ...
- JavaScript Basics_Fundamentals Part 1_Numbers
Javascript Numbers 知识描述:JavaScript 只有一种数字类型,即数字(Number).数字可以带小数点,也可以不带,也就是整数和小数. 数字可以带小数点,也可以不带: Exa ...
- JavaScript 转换数字为整数的方法
本文将会列举并说明JavaScript 把一个number(或者numerical的对象)转换成一个整数相关方法. 使用parseInt parseInt的语法如下:parseInt(string, ...
- JavaScript- The Good Parts CHAPTER 2
I know it well:I read it in the grammar long ago.—William Shakespeare, The Tragedy(悲剧:灾难:惨案) of Titu ...
- 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 ...
- PE刷题记
PE 中文翻译 最喜欢做这种很有意思的数学题了虽然数学很垃圾 但是这个网站的提交方式好鬼畜啊qwq 1.Multiples of 3 and 5 直接枚举 2.Even Fibonacci numbe ...
- Problem 21
Problem 21 https://projecteuler.net/problem=21 Let d(n) be defined as the sum of proper divisors of ...
随机推荐
- ECMAScript 6 proxies
在ECMAScript 5里面,可以通过(writable 和 configurable)内部属性把属性设置为不可修改和不可删除的,可以通过(Object.preventExtensions() )让 ...
- Linux特殊字符用法、后台命令管理
!! 重复前一个命令!字符 重复前一个以"字符"开头的命令!num 安装history命令的序号执行命令!?abc 重复之前包含"abc"的命令!-n 重复倒数 ...
- Java面试题-2
程序员面试之九阴真经 谈谈final, finally, finalize的区别: final:::修饰符(关键字)如果一个类被声明为final,意味着它不能再派生出新的子类,不能作为父类被继承.因此 ...
- 配置好postfix邮件服务器之后就可以使用它来发送邮件了
下面是一段摘自W3school关于php mail函数的栗子,经过测试发现两个问题. <?php $to = "somebody@example.com, somebodyelse@e ...
- ldap数据库--ldapsearch,ldapmodify
简单介绍一下ldapsearch命令,在ldap搜索条目时很有用,只要适当调整filter就可以. 命令如下: ldapsearch -h hostname -p port -b baseDN -D ...
- map的常用方法
1.头文件: #include<map> 2.定义: map<string,int>Map; 或: typedef map<string,int> MAP; MAP ...
- python 小白(无编程基础,无计算机基础)的开发之路 辅助知识1 with...as
这个语法是用来代替传统的try...finally语法的. with EXPRESSION [ as VARIABLE] WITH-BLOCK 基本思想是with所求值的对象必须有一个__enter_ ...
- [转载] Java学习之Hessian通信基础
转载自http://blog.sina.com.cn/s/blog_7f73e06d0100xn9j.html 一.首先先说Hessian是什么? Hessian:hessian是一个轻量级的r ...
- Python源码分析
- FPGA DDR3调试
FPGA DDR3调试 Spartan6 FPGA芯片中集成了MCB硬核,它可以支持到DDR3.在Xilinx的开发工具Xilinx ISE中提供了MIG IP核,设计者可以用它来直接生成 DDR3 ...