PROGRAM TO COUNT PAIRS (a, b) WHOSE SUM OF CUBES IS N (a^3+b^3 = N)
class Main
{
static int countPairs(int N)
{
int count = 0;
for (int i = 1; i <= Math.cbrt(N); i++)
{
int cb = i*i*i;
int diff = N - cb;
int cbrtDiff = (int) Math.cbrt(diff);
if (cbrtDiff*cbrtDiff*cbrtDiff == diff)
count++;
}
return count;
}
public static void main(String args[])
{
for (int i = 1; i<= 10; i++)
System.out.println("For n = " + i + ", " +
+ countPairs(i) + " pair exists");
}
}
OUTPUT
For n= 1, 1 pair exists
For n= 2, 1 pair exists
For n= 3, 0 pair exists
For n= 4, 0 pair exists
For n= 5, 0 pair exists
For n= 6, 0 pair exists
For n= 7, 0 pair exists
For n= 8, 1 pair exists
For n= 9, 2 pair exists
For n= 10, 0 pair exists
Comments
Post a Comment