% ---------------------
% Fermat's Last Theorem
% ---------------------
%
% Prabhakar Subrahmanyam - prasub@yahoo.com
% April/18/2002
% x^n+y^n=z^n for n>2
% ----------------------------------------------------------------------------
% Theorem: There are no positive integers x,y,z and n>2 such that x^n+y^n=z^n
% ----------------------------------------------------------------------------
% I had a discussion with my friend Dr. Anant of NASA regarding Fermat's enigma and perfect numbers, etc..
% He was arguing that it'd be impossible to write programs to find numbers that obey Fermat's therorem.
% I was arguing that it would be easy to write a program to go in a loop and ask it to print
% only when it obeys the condition x^n+y^n=z^n. He is correct, there are no numbers that obeys this condition
% and hence such a program will keep searching and choke the memory.
%
% Now, we know that for n=1, x^n+y^n=z^n would be 7^1+3^1=10^1, 2^1+3^1=5^1, etc...
% Power 1 is very easy, The sum of two numbers raised to power 1 equals the sum raised to power 1.
% So, in our program we'll calculate from n=2
%
% That was simple, the task at hand is to find such numbers that obeys Fermat's theorem.
% i.e, to figure out numbers, that is x^n+y^n=z^n where n=2,3,....
%
% ---------------------------------------------------------------------------------------------------
% Pierre de Fermat died in 1665. Today we think of Fermat as a number theorist, in fact as perhaps
% the most famous number theorist who ever lived. It is therefore surprising to find that Fermat was
% in fact a lawyer and only an amateur mathematician. Also surprising is the fact that he published
% only one mathematical paper in his life.
% ---------------------------------------------------------------------------------------------------
% Program begins - Prabhakar Subrahmanyam
clc % clear the screen before printing
clear all % clear all variables in memory
n=2;
maxout=50;
fprintf('\n\n\n\t\t\t\t\t\t\tF E R M A T'' S L A S T T H E O R E M \n')
fprintf('\t\t\t--------------------------------------------------------------------\n')
fprintf('\t\t\tFermat Last Theorem: (x^n)+(y^n)=(z^n)\n')
fprintf('\t\t\tThere are no positive integers x,y,z and n>2 such that x^n+y^n=z^n\n')
fprintf('\t\t\t--------------------------------------------------------------------\n')
fprintf('\nSearching for Fermat Numbers under%3d Natural integers...\n',maxout)
fprintf('Printing Fermat Numbers for n=%d\n',n)
fprintf('--------------------------------\n\n')
tbc=datestr(now);
fprintf('\nTime before computation:\t')
fprintf(tbc)
fprintf('\n\n')
%for n=2:5% Defining the power "n" here
% FermatWaitbar=waitbar(maxout,'Generating Fermat Numbers, Please wait...')
FermatWaitbar = waitbar(maxout,'Generating Fermat Numbers, Please wait...');
for ferma=maxout:maxout,
for x=1:maxout
for y=1:maxout
for z=1:maxout
fermatx=x.^n;
fermaty=y.^n;
fermatz=z.^n;
% fprintf('x=%d, y=%d, z=%d\r',x,y,z)
if ( (x^n)+(y^n)==(z^n) )
% fprintf('(%2d^%2d)+(%2d^%2d)=%2d^%2d\n',x,n,y,n,z,n)
fprintf('(%2d^%2d)+(%2d^%2d)=%2d^%2d :\t x=%2d, y=%2d, z=%2d and n=%2d\n',x,n,y,n,z,n,x,y,z,n)
end % for fermat condition
end % for z
end % for y
end % for x
%end % for n
%close(FermatWaitbar)
tac=datestr(now);
fprintf('\n Time before computation:\t')
fprintf(tbc)
fprintf('\n Time after computation:\t')
fprintf(tac)
% computation here %
waitbar(1/maxout)
end % for waitbar
close(FermatWaitbar)