'Sherlock and Squares 해커랭크' 알고리즘 문제의 요구사항을 분석하고 최적의 자료구조와 연산 방식을 적용해 구현한 풀이다.
Math 적절히 사용하면 한 줄 코드로 문제 해결아 가능하다.
Sherlock and Squares
주어진 수의 범위 안의 제곱근(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) { //sol2. one line code return (int) Math.floor(Math.sqrt(b)) - (int) Math.ceil(Math.sqrt(a)) + 1; }