We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

There is a bug with the C code, n has to be changed to unsigned long long to pass testcase 1 because otherwise it would be overflowing for some reason.

Here is a brief introspection on what the issue could be:

Let's say you have a variable c of type long long and variables a and b of type int. Now, if you type:

c=a*b

The expression on the RHS is evaluated first with type int, and then the result of that is casted to long long. But this is far too late, as the overflow with ints has already occured (if any), and then the overflowed answer is converted to long long. Instead, what you probably meant to be doing is:

As a side note, this is probably a pain to type in a longer expression, and therefore it is better to just declare N as long long initially, thus ensuring all arithmetic expressions are evaluated as long long.

## Project Euler #6: Sum square difference

You are viewing a single comment's thread. Return to all comments →

There is a bug with the C code,

`n`

has to be changed to`unsigned long long`

to pass testcase 1 because otherwise it would be overflowing for some reason.Here is a brief introspection on what the issue

could be:Let's say you have a variable c of type

long longand variables a and b of typeint. Now, if you type:The expression on the RHS is

evaluated first with type int, and then theresult of that is casted to long long. But this is far too late, as the overflow with ints has already occured (if any), and then the overflowed answer is converted to long long. Instead, what you probably meant to be doing is:As a side note, this is probably a pain to type in a longer expression, and therefore it is better to just declare N as long long initially, thus ensuring all arithmetic expressions are evaluated as long long.