2

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?

Anantha Raman
  • 197
  • 2
  • 11
  • Well the end values may not be perfect squares, but if you are making the assumption that both a and b are perfect squares, why not just add 2 to `n`? – Jeremy Fisher Aug 07 '15 at 15:32

1 Answers1

0

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

Lukabot
  • 189
  • 3
  • 11