Like Tree2Likes

Fermat's Last Theorem

This is a discussion on Fermat's Last Theorem within the C++ Programming forums, part of the General Programming Boards category; Hi guys, I copied the source code for a program which calculated the square roots of numbers, and then executed ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    13

    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 ). 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 Any suggestions would be most appreciated. Thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,745
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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.
    Last edited by MK27; 01-22-2012 at 08:22 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Jan 2012
    Posts
    13
    Thanks for the replies I'll look up arrays now...

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    2,581
    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.

    Link to a site about "Fermat's Last Theorem"
    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.
    Last edited by stahta01; 01-22-2012 at 08:51 AM.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  6. #6
    Registered User
    Join Date
    Jan 2012
    Posts
    13

  7. #7
    Registered User
    Join Date
    Jan 2012
    Posts
    13
    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; }

  8. #8
    Registered User
    Join Date
    Jan 2012
    Posts
    13
    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;
    }

  9. #9
    Registered User
    Join Date
    Sep 2010
    Posts
    7
    Quote Originally Posted by Branden Holmes View Post
    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.

    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.

  10. #10
    Registered User
    Join Date
    Jan 2012
    Posts
    13
    Thank you so much gratuitous! I'll fix the code up now....

  11. #11
    Registered User
    Join Date
    Jan 2012
    Posts
    13
    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;
    }

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,745
    A few comments:
    • 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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Jan 2012
    Posts
    13
    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.

  14. #14
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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.
    stahta01 likes this.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  15. #15
    Registered User
    Join Date
    Apr 2010
    Posts
    82
    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?
    stahta01 likes this.
    W7, Ubuntu -- mingw, gcc, g++, code::blocks, emacs, notepad++

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Futurama Theorem
    By Ferreres93 in forum C Programming
    Replies: 48
    Last Post: 12-05-2011, 10:27 AM
  2. Separating Axis Theorem
    By Kenki in forum Game Programming
    Replies: 20
    Last Post: 05-22-2005, 11:49 AM
  3. Pythagorans theorem
    By Dummies102 in forum C++ Programming
    Replies: 2
    Last Post: 02-20-2002, 12:46 PM
  4. Galois Theorem
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 01-06-2002, 05:20 PM
  5. David's CProgramming Theorem
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 08-14-2001, 12:29 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21