D Compilers Disprove Fermat’s Last Theorem
Jack Applegame
japplegame at gmail.com
Mon May 17 07:53:56 UTC 2021
I accidentally found an old funny article about the undefined
behavior in C / C++:
[C Compilers Disprove Fermat’s Last
Theorem](https://blog.regehr.org/archives/140)
After a little testing, I can confidently state that D compilers
disprove Fermat’s Last Theorem too!
```d
// fermat.d
import std.stdio;
bool fermat() {
const max = 1000;
int a = 1, b = 1, c = 1;
while(true) {
if(((a * a * a) == ((b * b * b) + (c * c * c)))) return true;
a++;
if(a > max) { a=1; b++; }
if(b > max) { b=1; c++; }
if(c > max) { c=1; }
}
return false;
}
void main() {
if(fermat()) writeln("Fermat's Last Theorem has been
disproved.");
else writeln("Fermat's Last Theorem has not been disproved.");
}
```
```shell
$ ldc2 -O2 --run fermat.d
Fermat's Last Theorem has been disproved.
```
Check it out on [run.dlang.io](https://run.dlang.io/is/TrO2aH)
More information about the Digitalmars-d
mailing list