将一个长数字分解为其组成整数时出现奇怪错误
Getting strange errors when breaking up a long number into its component integers
这是针对Project Euler的第8个问题,该问题要求您在这个非常长的数字中找到五个连续数字的最大乘积。
我的代码肯定是NOT最优雅的解决方案,但我确信它应该能工作。它似乎确实适用于小数字,但一旦我测试的数字大于16位,一切都开始分崩离析(例如,子集得到的是e和0以及不同的数字,而不是实际给定数字中的数字。)
function consecProduct(num,sec){
//converts the number to a string
var parser = num.toString();
var numLength = parser.length;
//prepares an array to hold 5 consecutive digits
var pieces = [];
var greatestProduct = 0;
var piecesTogether = 1;
// The outer loop that runs through each set of five digits
for (i=0; i<numLength-4; i++){
//fills a string with the five digit subset
var product = parser.substring(sec-5, sec);
console.log("product "+product);
//increments subset by 1
sec++;
//fills each array position with a each digit from subset
for(x=0;x<5;x++){
pieces[x]=product.substring(x,x+1);
console.log(x + " is "+ pieces[x]);
}
//converts each array digit back to an integer
for(x=0;x<5;x++){
pieces[x]=parseInt(pieces[x]);
}
console.log("hey");
//gets the product of the subset
for(x=0;x<5;x++){
piecesTogether = piecesTogether*pieces[x];
console.log(pieces[x] + " work " + piecesTogether);
}
//updates the greatestProduct
if ( piecesTogether > greatestProduct ){
greatestProduct = piecesTogether;
console.log("great product " + greatestProduct)
}
//resets the product for the next subset
piecesTogether = 1;
}
return greatestProduct;
}
console.log("hey");
consecProduct(111125455578788855,5);
我一直在用Codecademy的scratchpad测试它,也许这是问题的一部分。我上周刚开始学习js,昨天才开始学习这些欧拉问题,所以我可以用各种方法彻底解决这个问题。有什么想法吗?
只要您将数字作为字符串传递,就可以使用您的方法
但大多数浏览器都有一个forEach方法,可以为您简化它。
function eu8(s, n){
var max= 0, last= 0, A= s.split(''), L= A.length,
next, temp;
A.forEach(function(itm, i, A){
next= i;
temp= itm;
while(next<(i+n) && ++next<L) temp*= A[next];
if(temp> max){
max= temp;
last= i;
}
});
return [' Largest product in a sequence of '+n+' digits totals '+max+
','n found at digits #'+last+'-'+(last+n)+' : '+A.slice(last, last+n)];
}
// a shim for old browsers, (not needed with console):
if(!Array.prototype.forEach){
Array.prototype.forEach= function(fun, scope){
var T= this, L= T.length, i= 0;
if(typeof fun== 'function'){
while(i< L){
if(i in T){
fun.call(scope, T[i], i, T);
}
++i;
}
}
return T;
}
}
var s= '8383514919085125086820290424163504559356377168995032348562649291222000387486432845620761935475604819050366697920932015432273771435337266340072387705128115575935425014460947570294275818158944549440881025891661096019719598195504110300188717866666358085201663329077618987279717181749021476776048734274617619666392413744636813999541150937273597312043999174331828004915627872035802437409595473241982712379412840772356975718777505301009358387887491501687808639811743258849513533372548739871812190760522789399701735667528924543523146196411626759899045981351660803008793628326225793570101225880141881354855219845587323306406026446646995422604684079629891934580835393600990916331750430169147648113885025045982027652181257767798206409176994378464211282557774833632004180439443121563895765081630408290308927246861936209942841914894036534524282034126702443265629680626122703321065703277654006714223903324966372058553562951193965443957787594408861841150727372912209556865206484636763870595651959623483481581867874';
alert(eu8(s, 5));
//eu8(s, 5)
>>returned value:
Largest product in a sequence of 5 digits totals 204120,
first found at digits #535-540 : 7,5,9,8,9
我最近刚刚开始研究Project Euler,并实现了一个常规的for循环,而不是使用forEach方法。
function largestProduct(n, d){
var max = 0
for(var i = 0; i < n.length - d; i++){
var prod = 1;
for(var j = 0; j < d; j++){
prod *= Number(n.charAt(i+j));
}
if(prod > max){
max = prod;
}
}
return max;
}
相关文章:
- Node.js v6.2.0类扩展不是函数错误
- Jquery菜单操作不稳定,定位不正确,存在一般错误
- document.open/document.write没有正确地清除chrome中的文档——这是chrome的错误吗
- 试图在引导模式内动态生成图表,得到offsetWidth错误
- 为什么会出现错误;未捕获的类型错误:undefined不是函数;
- 我如何修复包含在captcha的addthis中的错误
- 错误:格式为整数的 javascript 键无效
- 如何在node.js中设置mysql连接,以在带无符号整数的算术上抛出超出范围的错误
- 错误:GM_setValue不支持的类型.支持的类型包括:字符串、布尔值和 32 位整数
- 我在 JavaScript 中将整数转换为文本时遇到错误
- AngularJS 表单验证 - 类型错误:无法设置未定义的属性“整数”
- 将一个长数字分解为其组成整数时出现奇怪错误
- angular.js解析整数错误
- 我如何避免或围绕地板函数和错误表示的整数工作
- Firebase App Script getData() optQueryParameters出现整数错误
- 对于以整数开头的id,无法识别的表达式出现错误
- 在javascript中,输入的电话号码应该只是整数,如果插入了除整数以外的任何东西,则会给出错误
- 为什么当我给他一个负数和一个正数时,我的 JS 随机整数生成器如此错误
- 条带错误 'exp_month' 参数应该是一个整数(而是未定义的)PHP / Stripe.js
- 字符串解析——为什么在javascript中将输入数组元素全部解析为整数时会出现运行时错误