TGarr
TGarr

Reputation: 47

Multiplying numbers in a string at specific positions

I'm trying to solve the 8th Project Euler problem.

Find the greatest product of five consecutive digits in the 1000-digit number.

I'm pretty sure I'm way off. I also tried a simpler program using a while loop that returned 1 every time. The code that I have provided also returns 1 every time.

Please don't provide a solution for me. Just give me a place to look if you can. Here is my 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 is my 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);

Upvotes: 1

Views: 634

Answers (3)

rliu
rliu

Reputation: 1148

You should try a more systematic approach to solving this problem instead of diving into the code all at once.

First, see if you could solve the problem, by hand, with a smaller input. So, for instance, take the first 10 digits of the original input string: 7316717653. Now try solving the problem on paper (maybe with a calculator, heh). It'd probably go something like 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 is how we might convert this to code. Let's start by writing down what we did above:

1) Start from the leftmost point
2) Take the next five numbers
3) Multiply them
4) Store the output
5) Shift to the right once
6) Repeat starting at 2), until there aren't five numbers left to process
7) Look through all of the products (the output of the multiplication)
8) Output the max

Now the challenge is to convert this into code. Since you don't want a solution, I leave this to you. But it seems like you are having some problems with the loop construct so perhaps I can give you a hint:

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

That snippet of code says:

1) Take a variable 'i' and set it to 1
2) While 'i' is less than or equal to 1000 execute "code here"
3) Increment 'i' by 5

So in the context of the problem you might want a loop to keep track of the current leftmost point. It would start at 0, it would increment by 1 each time, and it would stop five slots from the end, meaning:

...823257530420752963450
                   ^

there.

Lastly, I'll add that the code you would produce following my suggestion is sort of inefficient. But as you look through the code you'll find easy ways to make it faster, which feels awesome! Hopefully you get around to it. But keep in mind there are no time limits for project euler and the above solution will still run quite fast (probably a couple of seconds in javascript? Less than one in C for sure).

Upvotes: 1

Xotic750
Xotic750

Reputation: 23482

I'm not sure what can be said without example, which lead to a solution really.

What I would do is loop through the string, take a slice of 5 characters, split those 5 characters and convert them to numbers if necessary, find their product, check if it is the largest so far and store if they are, take the next 5, rinse and repeat.

I don't know why you have so many loops, when all you need is 1

no code displayed

A solution is on jsfiddle for those that wish to see

Upvotes: 1

Al_th
Al_th

Reputation: 1195

Just think of what is happening in your code.

You enter the 1st for loop. Then you enter the second one. You assign pos1 = j, and you loop (j is still <= 995).

The same happens for the 4 other loop. So each time prod = 996*997*998*999*1000

Upvotes: 1

Related Questions