Count Distinct Squares and Cubes Java

PROGRAM TO COUNT NUMBER OF DISTINCT SQUARES AND CUBES UP TO N



class MAIN 
{
  
// Function to count the number
// of perfect squares and cubes
static int countSC(int N)
{
    int res = (int)Math.sqrt(N) + 
              (int)Math.cbrt(N) - 
              (int)(Math.sqrt(Math.cbrt(N)));
  
    return res;
}
  
// Driver code
public static void main(String[] args) 
{
    int N = 20;
  
    System.out.println("Number of squares "
                            "and cubes is "
                                 countSC(N));
}
}

OUTPUT

Number of squares and cubes is 5

CREDITS: https://www.geeksforgeeks.org/program-to-count-number-of-distinct-squares-and-cubes-upto-n/

Comments

Popular posts from this blog

Solve the Sudoku Python

Solve the Sudoku Java

Find Duplicates Java