65.9K
CodeProject is changing. Read more.
Home

Perfect Cube

starIconstarIconstarIconemptyStarIconemptyStarIcon

3.00/5 (2 votes)

Apr 20, 2009

CPOL

1 min read

viewsIcon

26419

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

#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