# Fermat's last theorem and the LLVM compiler [closed]

The following blog:

http://blog.regehr.org/archives/140

discusses how the LLVM compiler seems to prove that Fermat's last theorem is false by showing that the compiler optimizes the $while(1)$ "check" code to the opcode $ret$.

The check code is given by:

#include <stdio.h>

int fermat (void) {
const int MAX = 1000;
int a=1,b=1,c=1;
while (1) {
if (((a*a*a) == ((b*b*b)+(c*c*c)))) return 1;
a++;
if (a>MAX) {
a=1;
b++;
}
if (b>MAX) {
b=1;
c++;
}
if (c>MAX) {
c=1;
}
}
return 0;
}
int main (void) {
if (fermat()) {
printf ("Fermat's Last Theorem has been disproved.\n");
} else {
printf ("Fermat's Last Theorem has not been disproved.\n");
}
return 0;
}


and returns: Fermat's Last Theorem has been disproved in LLVM GCC 4.2.

Many are questioning if LLVM truly disproved Fermat's last theorem by eliminating the $while(1)$ loop in its compile process or if it is because of a bug in LLVM.

So I replaced the while loop with something that I know should return theorem proved (the $a^2=b^2+c^2$ case):

while (1) {
if((a*a)==(b*b)+(c*c)) return 1;
a++;
if (a>MAX) {
a=1;
b++;
}
if (b>MAX) {
b=1;
c++;
}
if (c>MAX) {
c=1;
}
}


which returns: Fermat's Last Theorem has been disproved as well...

Would this confirm that there is effectively a bug in LLVM?

• Thus is not a theoretical computer science question. – Jeffε May 12 '14 at 2:22
• It's not a bug in LLVM. It's the result of some utter lunacy in the C++ standard, which says that compilers may assume all loops (which do not do I/O) terminate. – Neel Krishnaswami May 12 '14 at 10:54