In: Computer Science
C program
//In this assignment, we will find the smallest positive integer
that
// can be expressed as a sum of two positive cube numbers in two
distinct ways.
// More specifically, we want to find the smallest n such that n ==
i1*i1*i1 + j1*j1*j1,
// n == i2*i2*i2 + j2*j2*j2, and (i1, j1) and (i2, j2) are not the
same in the sense that
// not only (i1, j1) not euqal to (i2, j2), but also (i1, j1) not
equal to (j2, i2).
// We practice using loops in this assignment.
// Also, we practice how to do an early exit in a loop. We exit the
loop once we find the smallest n.
// Once we are done, we will be delighted surprised to find out
that this number already played a big role in
// our computer science study.
#include <stdio.h>
int main()
{
int i, j, n;
//We assume the smallest such n is no more than
1000000
for(n=1; n<=1000000; n++)
{
//Use count to record the number of
different ways of summing two positive cube numbers to n
int count = 0;
//TO DO
//Fill in code below
}
//Do not change code below
printf("%d is the solution:\n", n);
for(i=1; i<=100; i++)
for(j = i; j<= 100; j++)
{
if (i*i*i + j*j*j == n)
{
printf("%d * %d * %d + %d *
%d * %d = %d\n", i, i, i, j, j, j, n);
}
}
return 0;
//Do not try to hard code the solution, that is not
the way to learn.
}
Here is the code:
#include <stdio.h>
#include <math.h>
int main()
{
int i, j, n;
//We assume the smallest such n is no more than 1000000
for( n=1; n<=1000000; n++)
{
//Use count to record the number of different ways of summing two
positive cube numbers to n
int count = 0;
// find cube root of n using cbrt
for (int i = 1; i <= cbrt(n); i++)
for (int j = i + 1; j <= cbrt(n); j++)
if (i*i*i + j*j*j == n)
count++;
if (count == 2)
break;
}
//Do not change code below
printf("%d is the solution:\n", n);
for(i=1; i<=100; i++)
{
for(j = i; j<= 100; j++)
{
if (i*i*i + j*j*j == n)
{
printf("%d * %d * %d + %d * %d * %d = %d\n", i, i, i, j, j, j,
n);
}
} }
return 0;
//Do not try to hard code the solution, that is not the way to
learn.
}
OUTPUT:
1729 is the solution:
1 * 1 * 1 + 12 * 12 * 12 = 1729
9 * 9 * 9 + 10 * 10 * 10 = 1729
Logic is:
If a number X has pairs like (a,b) and (c,d) then as X<N, all
the numbers a,b,c,d must be less than cube root of N