Project Euler - Problem 5

I don’t want the solution of this problem.

Here’s my code : http://ideone.com/V2vYQZ

The code does not print any number. Is this due to some error in the code or is the if condition never satisfied in that range?

First of all, z is an array designed to hold only 19 entries. So, the valid set of indices are from 0…18

If you want to use 1…19, make sure you have proper size for the array.

1 Like

It would be good, if you also check your flow of control.

Sometimes, you think it will work the way you think, but in reality, the program won’t give expected output.

It is always a nice practice to double check everything! :smiley:

1 Like

your code in the ques is not printing ne thing since the loop breaks just after the 1st iteration…u have put a break statement outside the if statement…

instead of this…

if (p % z[1] == 0 && p % z[2] == 0 && p % z[3] == 0 && p % z[4] == 0 && p % z[5] == 0 && p % z[6] == 0 && p % z[7] == 0 && p % z[8] == 0 && p % z[9] == 0 && p % z[10] == 0 && p % z[11] == 0 && p % z[12] == 0 && p % z[13] == 0 && p % z[14] == 0 && p % z[15] == 0 && p % z[16] == 0 && p % z[17] == 0 && p % z[18] == 0 && p % z[19] == 0)
         printf ("%llu\n", p);
         break;

you need to do this…

if (p % z[1] == 0 && p % z[2] == 0 && p % z[3] == 0 && p % z[4] == 0 && p % z[5] == 0 && p % z[6] == 0 && p % z[7] == 0 && p % z[8] == 0 && p % z[9] == 0 && p % z[10] == 0 && p % z[11] == 0 && p % z[12] == 0 && p % z[13] == 0 && p % z[14] == 0 && p % z[15] == 0 && p % z[16] == 0 && p % z[17] == 0 && p % z[18] == 0 && p % z[19] == 0)
{
         printf ("%llu\n", p);
         break;
}

also the 1st number to satisfy this condition is 232792560 so ur loop will exceed time limit if u run it from 100000…hope this helps…:slight_smile:

2 Likes

@tijoforyou Why is this code http://ideone.com/7NgTHB working correctly? As correctly pointed out by you, z can only hold 19 integers so the valid index numbers should be till 18 only.

The address of an element a[i] is calculated as base-addr-of-a + i * size-of-each-element

In C/C++, there is no implicit bound-checks. So, it tries to access the location calculated by the above expression.

Sometimes, these locations can be accessed, and hence, there will not be an error. Sometimes, these locations should not be accessed (in which case, you get the famous Segmentation Fault or SIGSEGV).

Now, if there is no error, sometimes, we get right answer, sometimes, we don’t. The exact behaviour is undefined for this case.

In the given example, it just gave the right answer.

1 Like

@tijoforyou What could be the reason for producing no output?

There can be two options for not producing output.

The pgm actually prints something, but the standard output is not getting properly flushed, and nothing appears. This happens, though rarely, when you forget a new-line at the end of your printed data.

The second reason can be because, your print statements are not at all getting executed. This reason can be easily avoided by adding some debugging statements (some print instructions inserted into the code, just to see that the execution indeed reaches that point), to see if everything is working as intended!

@kunal361 Instead, I removed the break statement from the for loop but as expected it printed more than 1 result. With your code I would have got just the required integer. Thanks.

1 Like

glad could help…:slight_smile:

Precisely what I meant by double checking your flow of control!! :stuck_out_tongue:

Glad you found your mistake.

And the best thing I like is, you never ask “give me correct code”. And you try to find out the mistakes, with the help of the wonderful community we have.

Keep the good work going @gautam94

And good luck with Project Euler! :smiley:

1 Like