주어진 수의 범위 안의 제곱근(square integer)의 갯수를 구하는 문제이다. Watson likes to challenge Sherlock’s math ability. He will provide a starting and ending value that describe a range of integers, inclusive of the endpoints. Sherlock must determine the number of square integers within that range. Note: A square integer is an integer which is the square of an integer, e.g. 1,4,9.16,25
publicclassSherlockandSquares{ staticintsquares(int a, int b){ //sol1 int start = (int) Math.sqrt(a); int end = (int) Math.sqrt(b); start = (Math.pow(start, 2) >= a) ? start-1 : start;
publicclassSherlockandSquares{ staticintsquares(int a, int b){ //sol2. one line code return (int) Math.floor(Math.sqrt(b)) - (int) Math.ceil(Math.sqrt(a)) + 1; }