www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - D Compilers Disprove =?UTF-8?B?RmVybWF04oCZcw==?= Last Theorem

reply Jack Applegame <japplegame gmail.com> writes:
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)
May 17 2021
next sibling parent reply user1234 <user1234 12.de> writes:
On Monday, 17 May 2021 at 07:53:56 UTC, Jack Applegame wrote:
 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!
 ...
 Fermat's Last Theorem has been disproved.
 ```
 Check it out on [run.dlang.io](https://run.dlang.io/is/TrO2aH)
intersting but dmd -O does not disprove. Alos it's not about D, it's about backend optimizations, as expalined in the article.
May 17 2021
parent reply Dukc <ajieskola gmail.com> writes:
On Monday, 17 May 2021 at 08:24:22 UTC, user1234 wrote:
 intersting but dmd -O does not disprove.
 Alos it's not about D, it's about backend optimizations, as 
 expalined in the article.
To be more accurate, dmd-compiled code enters infinite loop, just as it should. Printing "fermat's theorem has not been disproven" would be just as wrong as LDC behaviour. It's surprising how so many backends can have the same optimizing bug even in 2021. Kudos for DMD doing the right thing.
May 18 2021
parent deadalnix <deadalnix gmail.com> writes:
On Tuesday, 18 May 2021 at 07:17:50 UTC, Dukc wrote:
 On Monday, 17 May 2021 at 08:24:22 UTC, user1234 wrote:
 intersting but dmd -O does not disprove.
 Alos it's not about D, it's about backend optimizations, as 
 expalined in the article.
To be more accurate, dmd-compiled code enters infinite loop, just as it should. Printing "fermat's theorem has not been disproven" would be just as wrong as LDC behaviour. It's surprising how so many backends can have the same optimizing bug even in 2021. Kudos for DMD doing the right thing.
Believe it or not, it is not a bug, at least not in C++. Consider the transforms as follow. 1/ What's after the loop is unreachable, therefore it can be removed. 2/ An analysis of the function shows that it has no side effects. 3/ Now the function must return true, granted that it returns at all. 4/ At the call site, we got a function with no side effect that can only return true, therefore we can constant fold the call to true. You will note that all the transformation are trivially correct, except 3, because we don't know if the function will return or not. This is where precise legalese of the language definition comes into play, as it turns out, in C++ - and C++ is by far the language people working on optimizer have focused the most - it is not mandatory to prove that the function will terminate. In a way, it does make sense, because providing that the function will terminate is equivalent to proving the halting problem, or, in this specific case, Fermat's last theorem, which is not something reasonable to expect from the optimizer. If the language asks you to prove this terminates, then the cost you pay is that many function that will terminate will not be optimized, because it is too difficult to prove it.
May 20 2021
prev sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Mon, May 17, 2021 at 07:53:56AM +0000, Jack Applegame via Digitalmars-d
wrote:
 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!
This reminds me of this thread: https://forum.dlang.org/post/mailman.3657.1591403118.31109.digitalmars-d puremagic.com in which DMD-compiled code is able to perceive quantum superimposition directly, as opposed to LDC-produced code that can only perceive one of the collapsed states at a time. :-D T -- Shin: (n.) A device for finding furniture in the dark.
May 17 2021