码迷,mamicode.com
首页 > 其他好文 > 详细

ProjectRuler 算法练习之 位数组成字符串相同的整数

时间:2014-06-28 08:13:17      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:style   blog   java   2014   os   javascript   

Problem :
It can be seen that the number, 125874, and its double, 251748, contain exactly the same digits, but in a different order.
Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits.

Implementation :


var sameDig = function f(n1,n2){

var strN1 = n1.toString();
var strN2 = n2.toString();


if(strN1.length != strN2.length) return false;


var arr1 = new Array();
var arr2 = new Array();


for(var i = 0;i < strN1.length; i++){
arr1.push(strN1[i]);
arr2.push(strN2[i]);
}


arr1.sort();
arr2.sort();


for(var i = 0;i < arr1.length; i++){ if(arr1[i]!=arr2[i]) return false;}


return true;


}


for(var i = 1;i < 999999; i++){
if(sameDig(i,2*i) &&
sameDig(i,3*i) &&
sameDig(i,4*i) &&
sameDig(i,5*i) &&
sameDig(i,6*i)) {console.log(i); break;}
}


ProjectRuler 算法练习之 位数组成字符串相同的整数,布布扣,bubuko.com

ProjectRuler 算法练习之 位数组成字符串相同的整数

标签:style   blog   java   2014   os   javascript   

原文地址:http://blog.csdn.net/lan_liang/article/details/35270939

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!