Click here to Skip to main content
13,589,560 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


9 bookmarked
Posted 20 Apr 2009
Licenced CPOL

Perfect Cube

Rate this:
Please Sign up or sign in to vote.
A Classic C++ Problem


About three days ago, I had a talk with a student about Math and Pierre de Fermat. It was an interesting discussion. His problem was how to solve the problem named Perfect Cube. So I had to take a small break with him and we resolved this simple-medium problem. I also found it interesting to write my conclusions.

For many years, Fermat Theorem, said that it was not possible for three integers number a,b,c > 1 and one natural number n > 2 to exist, in this way to have an + bn = cn, it has not been demonstrated. Many attempts have been made to demonstrate this thing and none of them worked. But it is possible to find integers greater then 1 that satisfy the perfect cube equation: a3 = b3 + c3 + d3 (for example 123 = 63 + 83 + 103 is true).


This problem aims to write a small program that will display in file "cubes.out" all multitudes of numbers {a,b,c,d} from multitude {1,2,...,100} that satisfy this equation like:

Cube = 6,   Triple = (3,4,5)
Cube = 12, Triple = (6,8,10)
Cube = 18, Triple = (2,12,16)
Cube = 99,   Triple = (11,66,88)
Cube = 100, Triple = (16,68,88)
Cube = 100, Triple = (35,70,85)

Using the Code 

For all pairs of natural numbers a and b, with properties: a > b and a, b being between 2 and 100, we are looking for c and d numbers with the property:

a3 - b3 = c3 + d3

We will use an array named cube to keep all cubes of natural numbers from [0,100]. For a and b fixed we note with rest the difference  a3 - b3. Later, for c fixed we look for d. In this way the cube[d] = rest - rest[c].

#include <stdio.h>

long cube[101];
FILE* fout;
void main()
         int i,a,b,c,d, rest, rest2;
         fout = fopen("cubes.out", "w");
         for(i=0; i<=100;i++)
              cube[i] = i*i*i;
         for(a=6; a<100; a++)
         for(b=2; b<a; b++)
             rest = cube[a] - cube[b];
             for(d=c; cube[d]<=rest2; d++)
                  fprintf(fout, "Cube=%d, Triple = (%d,%d,%d)\n",a,b,c,d);

Happy coding!


  • 20th April, 2009: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Marius Iulian Mihailescu
Instructor / Trainer Lumina - The University of South-East Europe
Romania Romania
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralFermat's Last Theorem Pin
drandand27-Apr-09 18:26
memberdrandand27-Apr-09 18:26 
GeneralRe: Fermat's Last Theorem Pin
Marius Iulian Mihailescu9-May-09 10:23
memberMarius Iulian Mihailescu9-May-09 10:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04 | 2.8.180615.1 | Last Updated 20 Apr 2009
Article Copyright 2009 by Marius Iulian Mihailescu
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid