Sherlock and Squares

Watson likes to challenge Sherlock's math ability. He will provide a starting and ending value that describes a range of integers, inclusive of the endpoints. Sherlock must determine the number of square integers within that range.

Example:

Input:  a=3,b=9
Output: 2

Approach

C++

#include <bits/stdc++.h>
using namespace std;

int squares(int aint b)
{
    return (floor(sqrt(b)) - ceil(sqrt(a)) + 1);
}

int main()
{
    int a = 3b = 9;
    cout << squares(ab);
    return 0;
}


No comments:

Post a Comment