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 ...
随机推荐
- AMD与commonJS
CommonJS:它是一个同步的模式.但是这种模式并不适合于浏览器端,如果浏览器同步模式一个一个加载模块,那么打开将会变得非常的慢. AMD:它最大的特点就是可以异步的方式加载模块,具体的不同在于AM ...
- 虚拟软件vmware安装
什么是虚拟软件: 虚拟原件是一个可以使你在一台机器上同时运行二个或更多Windows.LINUX等系统.它可以模拟一个标准PC环境.这个环境和真实的计算机一样,都有芯片组.CPU.内存.显卡.声卡.网 ...
- WebService-axis2
WebService框架有好多,常用的cxf,axis2等,axis2的配置过程相对简单,不用编写接口,在实现.只需要一个Service服务类即可.配置过程大致如下: 1,导入jar包(这里我是把ax ...
- Lucene全文检索引擎
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- 一个非常有用的函数—COALESCE
很多人知道ISNULL函数,但是很少人知道Coalesce函数,人们会无意中使用到Coalesce函数,并且发现它比ISNULL更加强大,不用再像以前 IsNull 又 IsNull(SqlServe ...
- [原创]阿里云RocketMQ踩过的哪些坑
由于公司的最近开始使用RocketMQ来做支付业务处理, 便开启了学习阿里云RocketMQ的学习与实践之路, 其中踩了不少的坑, 大部份是由于没有仔细查看阿里云的技术文档而踩的坑. 但是有一个非常大 ...
- 【Kafka源码】KafkaConsumer
[TOC] KafkaConsumer是从kafka集群消费消息的客户端.这是kafka的高级消费者,而SimpleConsumer是kafka的低级消费者.何为高级?何为低级? 我们所谓的高级,就是 ...
- Oracle与Sql server的区别
一直搞不明白Oracle数据库和sql server的区别,今天我特意查资料把他们的区别整理出来 Oracle数据库:Oracle Database,又名Oracle RDBMS,或简称Oracle. ...
- spring4+srpingmvc+mybatis基本框架(app后台框架搭建一)
前言: 随着spring 越来越强大,用spring4来搭建框架也是很快速,问题是你是对spring了解有多深入.如果你是新手,那么在搭建的过程中可以遇到各种各样奇葩的问题. SSM框架的搭建是作为我 ...
- Zabbix服务网页报错汇总
第1章 Zabbix简介及组成 1.1 zabbix简介 zabbix是一个基于web界面,提供分布式系统监视以及网络监视功能的企业级的开源解决方案.它可以监视各种网络参数,保证服务器自动的安全运营, ...