javascript - Multiplying numbers in a string at specific positions -


i'm trying solve 8th project euler problem.

find greatest product of 5 consecutive digits in 1000-digit number.

i'm pretty sure i'm way off. tried simpler program using while loop returned 1 every time. code have provided returns 1 every time.

please don't provide solution me. give me place if can. here code:

var bignum = "73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 85861560789112949495459501737958331952853208805511 12540698747158523863050715693290963295227443043557 66896648950445244523161731856403098711121722383113 62229893423380308135336276614282806444486645238749 30358907296290491560440772390713810515859307960866 70172427121883998797908792274921901699720888093776 65727333001053367881220235421809751254540594752243 52584907711670556013604839586446706324415722155397 53697817977846174064955149290862569321978468622482 83972241375657056057490261407972968652414535100474 82166370484403199890008895243450658541227588666881 16427171479924442928230863465674813919123162824586 17866458359124566529476545682848912883142607690042 24219022671055626321111109370544217506941658960408 07198403850962455444362981230987879927244284909188 84580156166097919133875499200524063689912560717606 05886116467109405077541002256983155200055935729725 71636269561882670428252483600823257530420752963450";  var pos1, pos2, pos3, pos4, pos5;  for(var i=1;i<=1000;i+=5) {    for(var j=0;j<=995;j++) {        pos1 = bignum[j];    }    for(var k=1;k<=996;k++) {        pos2 = bignum[k];    }    for(var l=2;l<=997;l++) {        pos3 = bignum[l];    }    for(var m=3;m<=998;m++) {        pos4 = bignum[m];    }    for(var n=4;n<=999;n++) {        pos5 = bignum[n];    }    prod = pos1 * pos2 * pos3 * pos4 * pos5;    if(prod>sum) {        sum = prod;    } } console.log(sum); 

here improved code:

var prod; var largest =1; for(var i=0;i<=995;i++) {    prod=num[i] * num[i +1] * num[i +2] * num[i +3] * num[i + 4];    if(prod>largest) {      largest = prod;    } }  console.log(largest); 

you should try more systematic approach solving problem instead of diving code @ once.

first, see if solve problem, hand, smaller input. so, instance, take first 10 digits of original input string: 7316717653. try solving problem on paper (maybe calculator, heh). it'd go this

7*3*1*6*7 = 882 3*1*6*7*1 = 126 1*6*7*1*7 = 294 6*7*1*7*6 = 1764 7*1*7*6*5 = 1470 1*7*6*5*3 = 630 1764 

the next step how might convert code. let's start writing down did above:

1) start leftmost point 2) take next 5 numbers 3) multiply them 4) store output 5) shift right once 6) repeat starting @ 2), until there aren't 5 numbers left process 7) through of products (the output of multiplication) 8) output max 

now challenge convert code. since don't want solution, leave you. seems having problems loop construct perhaps can give hint:

for(var i=1; <= 1000; i+=5) {     // code here } 

that snippet of code says:

1) take variable 'i' , set 1 2) while 'i' less or equal 1000 execute "code here" 3) increment 'i' 5 

so in context of problem might want loop keep track of current leftmost point. start @ 0, increment 1 each time, , stop five slots end, meaning:

...823257530420752963450                    ^ 

there.

lastly, i'll add code produce following suggestion sort of inefficient. through code you'll find easy ways make faster, feels awesome! around it. keep in mind there no time limits project euler , above solution still run quite fast (probably couple of seconds in javascript? less 1 in c sure).


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

javascript - addthis share facebook and google+ url -

ios - Show keyboard with UITextField in the input accessory view -