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.
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.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #6: Sum square difference
You are viewing a single comment's thread. Return to all comments →
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:
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.