일정한 수 범위 내에서 완전제곱수의 갯수를 세는 건데..
코드 보시면 a와 b에서 b에 루트값 버림, a에 루트값 올림
한 값의 차를 구하는데 이게 원리가 어떻게 되나요?
// An Efficient method to count squares between
// a and b
class CountSquares
{
double countSquares(int a,int b)
{
return (Math.floor(Math.sqrt(b)) -
Math.ceil(Math.sqrt(a)) + 1);
}
}
// Driver Code
public class PerfectSquares
{
public static void main(String[] args)
{
int a=9, b=25;
CountSquares obj=new CountSquares();
System.out.print("Count of squares is " +
(int)obj.countSquares(a, b));
}
}