Count Distinct Squares and Cubes Java
PROGRAM TO COUNT NUMBER OF DISTINCT SQUARES AND CUBES UP TO 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/
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
Post a Comment