Skip to content
Advertisement

Getting strange errors when breaking up a long number into its component integers

This is for Project Euler’s 8th question, which asks you to find the greatest product of five consecutive digits in this one really long number.

My code is definitely NOT the most elegant solution, but I’m pretty sure it should work. And it does seem to work with small numbers, but once the number I test is greater than 16 digits, everything starts to fall apart (e.g. The subsets are getting e’s and 0’s and different numbers instead of the digits in the actual given number.)

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);

I have been testing it with Codecademy’s scratchpad, maybe that is part of the problem. I just started learning js last week and only began these Euler problems yesterday, so I could be completely screwing this in a whole universe of ways. Any ideas?

Answer

As long as you pass the digits as a string you can use your method,

but most browsers have a forEach method that can simplify it for you.

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
Advertisement