Challenge
So, um, it seems that, while we have plenty of challenges that work with square numbers or numbers of other shapes, we don't have one that simply asks:
Given an integer n
(where n>=0
) as input return a truthy value if n
is a perfect square or a falsey value if not.
Rules
- You may take input by any reasonable, convenient means as long as it's permitted by standard I/O rules.
- You need not handle inputs greater than what your chosen language can natively handle nor which would lead to floating point inaccuracies.
- Output should be one of two consistent truthy/falsey values (e.g.,
true
orfalse
,1
or0
) - truthy if the input is a perfect square, falsey if it's not. - This is code-golf so lowest byte count wins.
Test Cases
Input: 0
Output: true
Input: 1
Output: true
Input: 64
Output: true
Input: 88
Output: false
Input: 2147483647
Output: false
18014398509481982
(2**54-2
), which is representable with a double, and causes answers that usesqrt
to fail. \$\endgroup\$ – Mego Jun 8 '17 at 12:242**54-2
is still larger than a double can safely handle, at least in JavaScript18014398509481982 > 9007199254740991
\$\endgroup\$ – Tom Jun 8 '17 at 12:322**54-2
into a JS console, and compare what you get with18014398509481982
(the exact value). JS outputs the exact value, therefore2**54-2
is representable with a double. If that still doesn't convince you, take the binary data0100001101001111111111111111111111111111111111111111111111111111
, interpret it as a IEEE-754 double-precision float, and see what value you get. \$\endgroup\$ – Mego Jun 8 '17 at 12:41