Search code examples
perfect-square

perfect square including both end values


I am trying to obtain all the perfect squares between two values(both included). I tried the following code which gives me the count excluding the end values.

cin>>a>>b; n=(int)sqrt(b)-sqrt(a);

How can i get the count of perfect squares including the end values?


Solution

  • Just add boundary condition to your logic

    how to fish - Pseudo code here

    1. n starts with 0
    2. if a || b is perfect square, n++
    3. n += (int)(sqrt(b) - sqrt(a))
    4. return n

    fish - here is the answer