This is perhaps the strangest thing I have ever come across: A number which is simultaneously positive and negative! (And I can prove it to you, because I have the link to my code with outputs/inputs here at ideaone. Basically, my output is a negative number, but even stranger: when I check to see if it is less than zero, it is false (?!). Even more strange: when you multiply it by a number other than one, it switches back to being printed as a positive number.
This error does not happen when I compile on Xcode, but it does when compiled on the internet (or with some other compilers), such as the one in my link.
It's not important to understand exactly what it does, I'm wondering why this value is both negative and positive at the same time.
#include <stdio.h>
#include <cmath>
int main()
{
unsigned long answer,T,n,N,a,d,b,L1,amount;
scanf("%ld",&T); // number of test cases to loop through
while (T--) {
scanf("%ld",&N);
amount = 0;
answer = 0;
n=N-N%2;
for (a = 1; a <= n/2; a++) {
d = N-a;
L1 = a*d;
for (b = 1; b*b < L1 ; b++) {
// "amount" is always a positive number
amount = 2*(((L1-1)/b) - b + 1) - 1;
if (d==a) answer+=amount;
else answer+=2*amount; // we are only adding positive values to this
}
}
if (answer<0) printf("This answer is less than zero %ld\n",answer);
if (answer>0) printf("This answer is greater than zero %ld\n",answer);
printf("%ld\n",answer);
printf("%ld\n",2*answer/2);
}
}
Input:
1
2500
Output:
Success time: 0.02 memory: 3300 signal:0
This answer is greater than zero -1842629629
-1842629629
304854019
As you can see, my negative answer is greater than zero. And not to mention, it's different from the answer when I compiled in Xcode, even in positive form.
I am amazed by this. It's printing out a negative number that is "positive"
Proof is here at ideaone.com