SAS code to prove Fermat's Last Theorem


Solar farm on SAS campus The full text of Fermat's statement, written in Latin, reads "Cubum autem in duos cubos, aut quadrato-quadratum in duos quadrato-quadratos, et generaliter nullam in infinitum ultra quadratum potestatem in duos eiusdem nominis fas est dividere cuius rei demonstrationem mirabilem sane detexi. Hanc marginis exiguitas non caperet."

The English translation is: "It is impossible for a cube to be the sum of two cubes, a fourth power to be the sum of two fourth powers, or in general for any number that is a power greater than the second to be the sum of two like powers. I have discovered a truly marvelous demonstration of this proposition that this margin is too narrow to contain."

Here at SAS, we don’t take challenges lightly. After a short but intensive brainstorming, we came up with a creative and powerful SAS code that effectively proves this long-standing theorem. And it is so simple and short that not only can it be written on the margins of this blog, it can be tweeted!

Drum roll, please!

Here is the SAS code:

data _null_; 
	do n=3 by 1; 
		do a=1 by 1; 
			do b=1 by 1; 
				do c=1 by 1; 
					e = a**n + b**n = c**n;
					if e then stop; 

Or written compactly, without unnecessary spaces:

data _null_;do n=3 by 1;do a=1 by 1;do b=1 by 1;do c=1 by 1;e=a**n+b**n=c**n;if e then stop;end;end;end;end;run;

which is exactly 112 character long – well below the Twitter 140-character threshold.

Don’t be fooled by the utter simplicity and seeming unfeasibility of this code.  For the naysayers, let me clarify that we run this code in a distributed multithreaded environment where each do-loop runs as a separate thread.

We also use some creative coding techniques:

1.     Do-loop with just two options, count= and by=, but without the to= option (e.g. do c=1 by 1;). It is a valid syntax in SAS and serves the purpose of creating infinite loops when they are necessary (like in this case). You can easily test it by running the following SAS code snippet:

data _null_;
	start = datetime();
	do i=1 by 1;
		if intck('sec',start,datetime()) ge 20 then leave;

The if-statement here is added solely for the purpose of specifying a wait time (e.g. 20) sufficient for persuading you in the loop’s infiniteness. Skeptics may increase this number to their comfort level or even remove (or comment out) the if-statement and enjoy the unconstrained eternity.

2.     Expression with two “=” signs in it (e.g. e = a**n + b**n = c**n;) Again, this is a perfectly valid expression in SAS and serves the purpose of assigning a variable the value of 0 or 1 resulting from a logical comparison operation. This expression can be rewritten as

e = a**n + b**n eq c**n;

or even more explicitly as

e = (a**n + b**n eq c**n);

As long as the code runs, the theorem is considered proven. If it stops, then the theorem is false.

You can try running this code on your hardware, at your own risk, of course.

We have a dedicated 128-processor UNIX server powered by an on-campus solar farm that has been autonomously running the above code for 40 years now, and there was not a single instance when it stopped running. Except pausing for the scheduled maintenance and equipment replacements.

During the course of this historic experience, we have accumulated an unprecedented amount of big data (all in-memory), converted it into event stream processing, and become a leader in data mining and business analytics.

This leads us to the following scientific conclusion: whether you are a pure mathematician or an empiricist, you can rest assured that Fermat's Last Theorem has been proven with a probability asymptotic to 1 beyond a reasonable doubt.

Have a happy 91-st day of the year 2017!



About Author

Leonid Batkhan

Leonid Batkhan, Ph.D. in Computer Science and Automatic Control Systems, has been a SAS user for more than twenty years. He came to work for SAS in 1995 and is currently a Senior Consultant with the SAS Federal Data Management and Business Intelligence Practice. During his career, Leonid has successfully implemented dozens of SAS applications and projects in various industries. All posts by Leonid Batkhan >>>


  1. Michael Raithel on


    OMG; this is as tremendous an achievement as it is a hilarious achievement!

    Well written!


    • Leonid Batkhan

      Andrew, as you know SAS software works on any platform. I have not tested this code specifically on the 32-bit versions of SAS/Studio, but I am guessing it will work, however with significantly diminished persuasion power. If you dare to run it in your environment please feel free to share your results with us.

  2. A few observations regarding the code: firstly - the 'c' loop will be infinite, so the 'a' and 'b' loops will never advance off '1'; secondly - since SAS uses 8-byte floating point for numerics the loop will eventually reach a value where it can't tell the difference between c and c+1, and therefore cease advancing the iterator; thirdly, - is there anyway to disable the session time limits? I couldn't find any.

    Fortunately, I give marks for effort . Try again next year.

    • Leonid Batkhan

      G'Day, Mr. Birch!
      Thank you so bloody much for making me fortunate to receive your marks for effort. Once a year, we can afford to have a few kangaroos loose in the top paddock. Your first argument that the 'a' and 'b' loops will never advance off '1' can be attributed to a too fast a reading and therefore missing the point. It is London to a brick that all the loops will run as described in this post: “For the naysayers, let me clarify that we run this code in a distributed multithreaded environment where each do-loop runs as a separate thread.” Secondly, when program will be approaching “a value where it can't tell the difference between c and c+1”, what would stop us from doubling numerics’ length to 16 bytes and let it run almost forever! Your third blue is not even an argument, it’s a question. And the answer to it is presented by the if-statement of the coding technique 1.
      Give it away, the date of this publication is reserved for going off.

  3. This proves it for the positive integer values in the real plane from 1 to as high as you have gone. Has this been tested on a continuum? What about with negative or complex values?

    • Leonid Batkhan

      Thank you, Brad, for your endorsement of the proof. Fermat's Last Theorem actually states that no three positive integers a, b, and c satisfy the equation a**n + b**n = c**n for any integer value of n greater than 2. Therefore continuum, negative and complex values are not covered by Fermat's Last Theorem. If you can formulate a new theorem for negative and complex values, we will make our best efforts to title our next year April 1st post "SAS code to prove Morantz's Last Theorem".

  4. Tomás Cámara on

    The story is nice indeed. But would a mathematician consider this a proof? Waiting for infinity is not a human's option..
    How frustrating, if the day after passing away the infinite iteration stops being infinite to find its first counterexample.

    • Leonid Batkhan

      Thank you, Tomás, for your comment. Mathematicians are also humans. They love, they laugh, they err, and they are mortal. They live by delusional rules of logic which sometimes lead them to a dead end. In the real world of relativity, what once considered to be a "settled science" some day may become a prejudice. It is not frustrating when one's beliefs get dispelled the day after passing away, it would have been frustrating when it happened the day before...

  5. That, by far, is the most elegant SAS code I've seen. Simple to read, compact, and powerful. If it stops at some point, I won't be around to see it. 🙂

  6. So that server that's been running for 40 years... I take it that started as a less than 128-core machine? Is it still running? Have there been any issues with numerical value ranges?

    • Leonid Batkhan

      You are correct, Paul. It started as a less than 128-core machine. But my imagination settled on 128. It is still running as a condition of the theorem to be true. Happy belated April's Fools Day!

  7. Stipulating that the code runs on a "distributed multithreaded environment where each do-loop runs as a separate thread", the number of cores will always be MUCH less than infinity, so how can this (empirically) prove Fermat's Last Theorem? The concept of limit might apply... I do not have access to SAS on such a system, but I would be interested what values of N, A, and B the processes might generate. It appears to me, naively, that only C will iterate.

Leave A Reply

Back to Top