Perfect square or not?

匆匆过客 提交于 2019-11-28 04:12:26

Because all perfect squares are sums of consecutive odd numbers:

  • 1 = 1
  • 4 = 1 + 3
  • 9 = 1 + 3 + 5
  • 16 = 1 + 3 + 5 + 7

and so on. Your program attempts to subtract consecutive odd numbers from n, and see if it drops to zero or goes negative.

You can make an informal proof of this by drawing squares with sides of {1,2,3,4,...} and observe that constructing a square k+1 from square k requires adding 2k+1 unit squares.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!