# Fermat's Last Theorem

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 01-22-2012
Branden Holmes
Fermat's Last Theorem
Hi guys,

I copied the source code for a program which calculated the square roots of numbers, and then executed the program in Code::Blocks. Seeing it calculating the numbers gave the idea of trying to disprove Fermat's Last Theorem (which has already been proven :tongue:). But when I had a look at how I could write the code I was dismayed to find that there are only five operators (addition, multiplication etc.). I need to use indices (i.e. 2^3) to try and work out a solution to the following equation:

a^? + b^? = c^?

Where ? = the same number each time, and must be 3 or greater.

I had the idea of doing the following:

1*1*1 + 2*2*2= c*c*c (or c^3)

Which may work, but I need to find a whole number solution for it. So I kinda need some help (ok a lot of help....) writing the program to find a solution and become the greatest non-mathematical mathematician in the world :cool: Any suggestions would be most appreciated. Thanks.
• 01-22-2012
laserlight
You can make use of what is available from <cmath>, but that is for floating point numbers. You might as well try your hand at writing a function yourself to raise an integer to an integral power.
• 01-22-2012
MK27
I don't have much formal math, but aren't indices normally subscripted? In any case, you can have arrays of numbers in C++ and use indexes to the array. If you don't know what arrays are, time to learn, so ask. If you do and think this is not a solution, all apologies.

If by c^3 you mean "c to the power of 3" you would:

Code:

`#include <cmath>`
Nb, you also need a linker flag for this -- and then you have access to functions like std :: pow().

Common mathematical functions - Cppreference

You can get around the float issue via type casting.
• 01-22-2012
Branden Holmes
Thanks for the replies :) I'll look up arrays now...
• 01-22-2012
stahta01
Wish you luck, I spent many many hours try to disprove Fermat's Last Theorem as a kid in High School.
And, some hours after High School. I finally decided it was beyond me.

Fermat's Last Theorem -- from Wolfram MathWorld

Tim S.

PS: I really hope I do NOT waste any more thinking time on this subject.
It approached being a obsession for me.
• 01-22-2012
Branden Holmes
• 01-22-2012
Branden Holmes
I found the following program here but when I tried to execute it there were errors. I modified the program until their weren't any errors and the program executed properly. However, as soon as I pressed any key the program shut down automatically. Can anybody please tell me how to modify it so that it actually works? Thanks in advance.

Code:

```#include <iostream.h> #include <conio.h> #include <math.h> int main() {         long n=1,i,j,flag,s,k;  //initializing         long p=3;              //replace 'p' to check for any other power         while(1)                //loop till infinity     {                 flag=0;k=pow(n,p);                 for(i=1;pow(i,p)<k;i++)        //looping first number                 {                         for(j=i+1;pow(i,p)+pow(j,p)<=k;j++) //looping second number                         {                           s=pow(i,p)+pow(j,p);                           if(s==k){flag++;}                           if(flag==2){break;}          //break out of loop if found                         }                         if(flag==2){break;}                 }           if(flag==2){cout<<n;break;}  //print number if found and the program terminates           n++;         } return 0;          } ```
• 01-23-2012
Branden Holmes
I edited the 1st and 3rd "#include" preprocessors and deleted the 4th and 5th last lines of code (because my printer has no ink). I can execute the program and it doesn't close as soon as I press a button now, which is good. But now it won't let me input any information. Any help would be very much appreciated. Thanks.

Code:

```#include <iostream> #include <conio.h> #include <cmath> int main() {         long n=1,i,j,flag,s,k;  //initializing         long p=3;              //replace 'p' to check for any other power         while(1)                //loop till infinity     {                 flag=0;k=pow(n,p);                 for(i=1;pow(i,p)<k;i++)        //looping first number                 {                         for(j=i+1;pow(i,p)+pow(j,p)<=k;j++) //looping second number                         {                           s=pow(i,p)+pow(j,p);                           if(s==k){flag++;}                           if(flag==2){break;}          //break out of loop if found                         }                         if(flag==2){break;}                 }         } return 0; }```
• 01-24-2012
gratuitous_arp
Quote:

Originally Posted by Branden Holmes
I edited the 1st and 3rd "#include" preprocessors and deleted the 4th and 5th last lines of code (because my printer has no ink)

The print statement will not print a message to your printer, it will print to your monitor. The answer the program comes up with needs to be printed (to your monitor) or you will never see the result.

I haven't read fully through the problem you're trying to solve and the code you're using to try to solve it, so I can't tell you if the code will give you the result you are looking for when functioning as intended. However, I do know that without these lines of code:

Code:

```if(flag==2){cout<<n;break;}  //print number if found and the program terminates n++;```
the program has an infinite loop that won't terminate, and the program won't do anything useful.

If those lines were deleted because you were getting an error similar to: "cout was not declared in this scope", then type "std::" before cout (no spaces) and recompile.

Quote:

But now it won't let me input any information.
The program wasn't designed to allow you to enter information while it is running. The comments in the code say to change the initial value of "p" to be whatever you want (3, 4, 5...), then recompile and run the code to check a different value of p.
• 01-25-2012
Branden Holmes
Thank you so much gratuitous! I'll fix the code up now....
• 01-25-2012
Branden Holmes
Thanks to gratuitous, I got the program working. For anybody else who may be interested in trying their luck, here is the exact code:

Code:

```#include <iostream> #include <conio.h> #include <cmath> int main() {     long n = 1,i,j,flag,s,k;  //initializing     long p = 3;              //replace 'p' to check for any other power     while(1)                //loop till infinity     {         flag=0;k=pow(n,p);         for(i=1;pow(i,p)<k;i++)        //looping first number         {             for(j=i+1;pow(i,p)+pow(j,p)<=k;j++) //looping second number             {               s=pow(i,p)+pow(j,p);               if(s==k){flag++;}               if(flag==2){break;}          //break out of loop if found             }             if(flag==2){break;}         }         if(flag==2){std::cout<<n;break;}  //print number if found and the program terminates         n++;     } return 0; }```
• 01-25-2012
laserlight
• You made an effort at indentation, which is good, but it can still be improved.
• You are not using anything from <conio.h>, so remove it, especially since it is non-standard.
• You should have a using std::pow; somewhere, since pow from <cmath> is, by right, in the std namespace.
• Declare variables near first use. For example, you could declare i and j in the initialisation statements of the respective loops.
• Use descriptive comments. Your comment about "initializing" is not very useful, and in fact it is incorrect since you only explicitly initialised one variable in that line.
• Use descriptive variable names. In this case, perhaps because of mathematical brevity, you used many single variable names. Okay, then use appropriate comments to tell the reader what the variables are for. Your variable named flag does not tell us what it means for the flag to be set, and it fact it turns out that flag is a count, not a flag.
• Instead of while(1), use while (true).
• Generally, individual expression statements should be kept on their own lines.
• In the inmost loop, you computed pow(i,p)+pow(j,p) twice. A compiler might be able to optimise that, but that is iffy. Rather, you could have written (s = pow(i, p) + pow(j, p)) <= k as the condition.
• If you can replace the controlled infinite loop with a while loop, do so.
• It does not matter here, but generally, prefer pre-increment to post-increment because pre-increment is normally no worse than post-increment, and could even be more efficient.
• 01-25-2012
Branden Holmes
Thankyou very much for the criticisms laserlight, but I didn't write this program, I got it off the internet :) But I shall edit the code as you suggested.
• 01-25-2012
anon
There are more problems.

Converting the result of pow, which is a floating point value, to integer may cause rounding errors, e.g 10003 may well come out as 999,999,999. You would be much better off writing your own pow function that works with integers.

While there is an infinite loop, built-in types (including long) can only represent numbers in a range with finite size. If the result of a computation is larger than that, the result is undefined. Any results after you overflow the limit are meaningless.

The test itself could be made much more efficient. E.g, to find a and b, so that a3+ b3 = 10003, does it really make sense to start with testing 13 + 23? Wouldn't a single test with 13 + 993 be enough, then 23 + 993 etc, until the result is larger than 1003 in which case you switch to 983 etc.

That combined with avoiding overflowing integers would let you get meaningful results at least for that range.
• 01-25-2012
Ocifer
I hope I'm not out of line, but how has this gone on so long? I see new posters here trying to learn c++ concepts with contrived examples, or trying things that would be better suited to other languages and they get TORN apart.

What am I missing? FLT has been proven. Hence the T. Do words mean ANYTHING? I knew this problem was a fetish for mathematicians, but isn't it over now?
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last