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

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;

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;



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

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:



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

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

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

Need Your Help

CAS JpaTicketRegistry not working

java hibernate jpa serialization cas

I'm trying to install a CAS server version 4.0.0 instance, and configuring the ticket registry. The problem I'm facing happens with the JpaTicketRegistry when I try to login.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.