Click here to Skip to main content
15,890,336 members
Articles / Programming Languages / C++

Perfect Cube

Rate me:
Please Sign up or sign in to vote.
3.00/5 (2 votes)
20 Apr 2009CPOL1 min read 26K   9   2
A Classic C++ Problem

Introduction

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).

Background 

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].

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++)
             if(cube[d]==rest2)
                  fprintf(fout, "Cube=%d, Triple = (%d,%d,%d)\n",a,b,c,d);
         }
         fclose(fout);
}

Happy coding!

History

  • 20th April, 2009: Initial post

License

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


Written By
Team Leader
Romania Romania
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralFermat's Last Theorem Pin
drandand27-Apr-09 18:26
drandand27-Apr-09 18:26 
GeneralRe: Fermat's Last Theorem Pin
Marius Iulian Mihailescu9-May-09 10:23
professionalMarius 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.