Pair Cube Count Java

PROGRAM TO COUNT PAIRS (a, b) WHOSE SUM OF CUBES IS N (a^3+b^3 = N)


class Main
{
    // method to count the pairs satisfying
    // a ^ 3 + b ^ 3 = N
    static int countPairs(int N)
    {
        int count = 0;
       
        // Check for each number 1 to cbrt(N)
        for (int i = 1; i <= Math.cbrt(N); i++)
        {
            // Store cube of a number
            int cb = i*i*i;
       
            // Subtract the cube from given N
            int diff = N - cb;
       
            // Check if the difference is also
            // a perfect cube
            int cbrtDiff = (int) Math.cbrt(diff);
       
            // If yes, then increment count
            if (cbrtDiff*cbrtDiff*cbrtDiff == diff)
                count++;
        }
       
        // Return count
        return count;
    }
      
    // Driver method
    public static void main(String args[]) 
    {
        // Loop to Count no. of pairs satisfying
        // a ^ 3 + b ^ 3 = i for N = 1 to 10
        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

Popular posts from this blog

Solve the Sudoku Python

Solve the Sudoku Java

Find Duplicates Java