首页 > 代码库 > Project Ruler 算法练习之 10 进制 转 2进制 以及数字对称
Project Ruler 算法练习之 10 进制 转 2进制 以及数字对称
问题描述:
The decimal number, 585 = 10010010012 (binary), is palindromic in both bases.
Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2.
(Please note that the palindromic number, in either base, may not include leading zeros.)
1.找出十进制对称的数字
2.在十进制对称的数字转2进制找出2进制也对称的数字
(function(){ var isPalin = function (n){ var strN = n.toString(); if(strN.length < 2)return false; for(var i =0 ;i <= n/2;i++){if(strN[i] != strN[strN.length-1-i]) return false;} return true; }; var toBinary = function tb(n){ if(this.ret == undefined) this.ret = ""; var tmp = n / 2 | 0; this.ret = (n % 2).toString() + this.ret; if(tmp == 1){var r = "1" + this.ret; this.ret = ""; return r;} else{return tb(tmp);} }; for(var i = 1; i< 300 ;i ++){ if(isPalin(i) && isPalin(toBinary(i))) console.log (i); } })();
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。